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

xiaoxiao2021-03-01  16

## 类型相等性

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的类型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