01-go数据类型

本文介绍了Go语言的多种数据类型。包括通过type定义的类型、整数类型、浮点数、复数类型等。还说明了byte与rune的别名关系,字符串的两种表示法,以及字典、通道等类型的特点,如通道需用make创建,重复关闭会引发异常等。

###1、通过type定义的类型,与原类型不同。

###2、计算架构相关的整数类型

int,uint

###3、显式表达自身宽度的整数类型

int8,int16,int32,int64
uint8,uint16,uint32,uint64

###4、浮点数

浮点数类型有两个:float32/float64
###5、复数类型

复数类型有两个:complex64/complex128
###6、byte与rune

type byte = uint8

//byte is an alias for uint8 and is equivalent to uint8 in all ways.
//It is used, by convention, to distinguish byte values from 8-bit unsigned integer values.

byte是uint8的别名,两者完全等价。定义byte,只是因为按照惯例,使对字节值和8位无符号整数值进行区分。

####type rune = int32

rune is an alias for int32 and is equivalent to int32 in all ways. It is used, by convention, to distinguish character values from integer values.

rune是int32的别名,两者完全等价。定义rune,只是因为按照惯例,将字符值与整数值进行区分。

gofrontend\go\gogo.cc

// "byte" is an alias for "uint8".
  uint8_type->integer_type()->set_is_byte();
  Named_object* byte_type = Named_object::make_type("byte", NULL, uint8_type,
                loc);
  byte_type->type_value()->set_is_alias();
  this->add_named_type(byte_type->type_value());

// "rune" is an alias for "int32".
  int32_type->integer_type()->set_is_rune();
  Named_object* rune_type = Named_object::make_type("rune", NULL, int32_type,
                loc);
  rune_type->type_value()->set_is_alias();
  this->add_named_type(rune_type->type_value());

###7、unsafe.Pointer、uintptr

src\builtin\builtin.go

// uintptr is an integer type that is large enough to hold the bit pattern of

// any pointer.

type uintptr uintptr
src\unsafe\unsafe.go

type ArbitraryType int

type Pointer *ArbitraryType

//- A pointer value of any type can be converted to a Pointer.
//- A Pointer can be converted to a pointer value of any type.
//- A uintptr can be converted to a Pointer.
//- A Pointer can be converted to a uintptr.

两组可以相互转转换类型
1、Pointer、任意指针类型
2、Pointer、uintptr
###8、别名

src\builtin\builtin.go

type IntegerType int

type Type int

type FloatType float32

type ComplexType complex64

###9、字符串

字符串的表示法有两种:原生表示法和解释型表示法。

原生表示法:反引号"`"包裹字符序列。不会对/t等做转义。

解释型表示法:双引号"""包裹字符序列。

字符串值是不可变。因为字符串底层是一个指针,可能指向一个常量。
###10、字典类型

map[K]T

字典的键类型必须是可比较的
###11、通道类型

chan T

与其他的数据类型不同,我们无法表示一个通道类型的值,因此,我们无法用字面量来为通道类型的变量赋值。只能通过调用内建函数make来达到目的。

操作符<-向通道值发送数据.

value, ok := <- ch1

ok的值是bool类型的,代表通道值的状态。

true:通道值有效

false:通道值已无效(或称已关闭)

对通道值的重复关闭会引发运行时异常,会使程序崩溃。

默认情况下,通道都是双向的,即双向通信。

接收通道:type Receiver <- chan int

发送通道:type Sender char <- int

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值