0-数据类型

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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容