Go语言规范-类型与值的特性

xiaoxiao2021-03-01  5

Go语言规范-类型与值的属性

类型相等性可赋值性可表示性

类型相等性

两个类型不是相同,就是不同。

一个被定义的类型总是与其他类型不同。反之,两个类型只有在它们的底层类型字面从结构上相等时,才是相同的; 即,它们具体相同的字面结构且相应的组件有相同的类型。详情如下:

两个数组类型当它们的元素类型相同且数组的长度相同,它们就是相同的。 两个切片类型当它们的元素类型相同,它们就是相同的两个结构类型当它们有相同的字段顺序,相应的字段具有相同的名称、类型、标签,它们是相同的。来自不同包的非导出字段名总是不同的。两个指针类型当它们有相同的基类型,它们就是相同的。两个函数类型当它们具有相同数量参数和返回值,相应的参数和返回值类型也相同,它们就是相同的。不要求参数和返回值的名称要匹配。两个接口类型当它们具有相同的方法集,它们就是相同的。来自不同包的非导出方法名总是不同的。方法的顺序是无关的。两个映射类型如果它们具有相同的key和元素类型,它们就是相同的。 两个通道类型如果具有相同的元素类型和相同的方便,它们就是相同的。

Given the declarations

type ( A0 = []string A1 = A0 A2 = struct{ a, b int } A3 = int A4 = func(A3, float64) *A0 A5 = func(x int, _ float64) *[]string ) type ( B0 A0 B1 []string B2 struct{ a, b int } B3 struct{ a, c int } B4 func(int, float64) *B0 B5 func(x int, y float64) *A1 ) type C0 = B0

these types are identical:

A0, A1, and []string

A2 and struct{ a, b int }

A3 and int

A4, func(int, float64) *[]string, and A5

B0 and C0

[]int and []int

struct{ a, b *T5 } and struct{ a, b *T5 } -func(x int, y float64) *[]string, func(int, float64) (result *[]string), and A5

B0 and B1 are different because they are new types created by distinct type definitions; func(int, float64) *B0 and func(x int, y float64) *[]string are different because B0 is different from []string.

可赋值性

当满足以下条件时,值x可赋值给类型为T的变量(“x可赋值给 T”) :

x的类型与T相同. x的类型V与T具有相同的底层类型并且V或T至少有一个不是被定义的类型。 T是一个接口类型且x实现了T. x是一个双向通道值,T是一个通道类型,x的类型V和T具有相同的元素类型且V和T至少有一个不是被定义的类型。 x是预定义的标识符nil而T是一个指针、函数、切片、映射、通道、或接口类型。 x是一个无类型常量且可以被表示成T类型的值。

可表示性

A constant x is representable by a value of type T if one of the following conditions applies:

x is in the set of values determined by T.T is a floating-point type and x can be rounded to T’s precision without overflow. Rounding uses IEEE 754 round-to-even rules but with an IEEE negative zero further simplified to an unsigned zero. Note that constant values never result in an IEEE negative zero, NaN, or infinity.T is a complex type, and x’s components real(x) and imag(x) are representable by values of T’s component type (float32 or float64). x T x is representable by a value of T because 'a' byte 97 is in the set of byte values 97 rune rune is an alias for int32, and 97 is in the set of 32-bit integers "foo" string "foo" is in the set of string values 1024 int16 1024 is in the set of 16-bit integers 42.0 byte 42 is in the set of unsigned 8-bit integers 1e10 uint64 10000000000 is in the set of unsigned 64-bit integers 2.718281828459045 float32 2.718281828459045 rounds to 2.7182817 which is in the set of float32 values -1e-1000 float64 -1e-1000 rounds to IEEE -0.0 which is further simplified to 0.0 0i int 0 is an integer value (42 + 0i) float32 42.0 (with zero imaginary part) is in the set of float32 values x T x is not representable by a value of T because 0 bool 0 is not in the set of boolean values 'a' string 'a' is a rune, it is not in the set of string values 1024 byte 1024 is not in the set of unsigned 8-bit integers -1 uint16 -1 is not in the set of unsigned 16-bit integers 1.1 int 1.1 is not an integer value 42i float32 (0 + 42i) is not in the set of float32 values 1e1000 float64 1e1000 overflows to IEEE +Inf after rounding
转载请注明原文地址: https://www.6miu.com/read-3850220.html

最新回复(0)