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

原文:http://golang.org/doc/go_spec.html
    翻译:红猎人 (zengsai@gmail.com)

Properties of types and values
类型和值的属性

Two types are either identical or different, and they are either compatible or incompatible. Identical types are always compatible, but compatible types need not be identical.

两个类型要么相同要么不相同,要么它们兼容要么不兼容。相同的类型总是兼容的,但兼容的类型不一定相同。

Type identity and compatibility
类型一致性和兼容性

Type identity
类型一致性

Two named types are identical if their type names originate in the same type declaration (§Declarations and scope). A named and an unnamed type are never identical. Two unnamed types are identical if the corresponding type literals have the same literal structure and corresponding components have identical types. In detail:
如果两个类型有相同的创建类型声明,那么这两个类型相同。命名的类型和未命名的类型是不相同的。如果两个未命名的型具有相同的结构而且对应的组件具有相同的类型,那么这两个类型相同。详细:

Two array types are identical if they have identical element types and the same array length.
Two slice types are identical if they have identical element types.
Two struct types are identical if they have the same sequence of fields, and if corresponding fields have the same names and identical types. Two anonymous fields are considered to have the same name.
Two pointer types are identical if they have identical base types.
Two function types are identical if they have the same number of parameters and result values and if corresponding parameter and result types are identical. All "..." parameters without a specified type are defined to have identical type. All "..." parameters with specified identical type T are defined to have identical type. Parameter and result names are not required to match.
Two interface types are identical if they have the same set of methods with the same names and identical function types. The order of the methods is irrelevant.
Two map types are identical if they have identical key and value types.
Two channel types are identical if they have identical value types and the same direction.

如果两个数组的长度和元素类型相同,那么它们相同。
如果两个切片有相同的元素类型,那么它们相同。
如果两个结构体有相同的成员变量顺序,而且相应的成员名称相同,而且相应的成员类型相同,那么它们相同。两个结构体中的匿名成员被认为有相同的名字。
如果两个指针有相同的基类型,那么它们相同。
如果两个函数参数个数和返回值个数相同而且参数和返回值对应的类型相同,那么它们相同。所有没有指定类型的“...‘参数被认为是相同的。所有指定类型T的“...‘参数被认为是相同的。参数和返回值的名称不要求相同。
如果两个接口有相同的方法集并且方法名称和函数类型相同,那么它们相同。方法的顺序不要求相同。
如果两个 map 有相同的键值类型,那么它们是相同的。
如果两个 channel 有相同的值类型并且方向相同,那么它们是相同的。

Type compatibility
类型兼容性

Type compatibility is less stringent than type identity: a named and an unnamed type are compatible if the respective type literals are compatible. In all other respects, the definition of type compatibility is the same as for type identity listed above but with ``compatible'' substituted for ``identical''.
类型兼容没有类型相同那么严格:名称和未命名的类型如果创建类型兼容,那么它们兼容。除此之外,类型兼容性的定义就和上面讲的类型一致性相同,只是把相同换为兼容。

Given the declarations
给出如下声明

type (
T0 []string
T1 []string
T2 struct { a, b int }
T3 struct { a, c int }
T4 func(int, float) *T0
T5 func(x int, y float) *[]string
)
these types are identical:
这些是相同的:

T0 and T0
[]int and []int
struct { a, b *T5 } and struct { a, b *T5 }
func(x int, y float) *[]string and func(int, float) (result *[]string)
T0 and T1 are neither identical nor compatible because they are named types with distinct declarations.
T0 和 T1 不再是相同的类型,也不再兼容,因为它们被声明为不同的类型名。

These types are compatible:
这些是兼容的:

T0 and T0
T0 and []string
T3 and struct { a int; c int }
T4 and func(x int, y float) *[]string
T2 and struct { a, c int } are incompatible because they have different field names.
T2 和 struct { a, c int } 不兼容,因为它们有成员名称不同。

Assignment compatibility
赋值兼容性

A value v of static type V is assignment compatible with a type T if one or more of the following conditions applies:
如果满足以下条件,那么 V 类型的变量 v 可以与 T 类型变量 赋值相容(即可以把v赋值给T类型的变量)。

V is compatible with T.
T is an interface type and V implements T.
V is a pointer to an array and T is a slice type with compatible element type and at least one of V or T is unnamed. After assignment, the slice variable refers to the original array; the elements are not copied.
V is a bidirectional channel and T is a channel type with compatible element type and at least one of V or T is unnamed.

V与T兼容。
T是一个接口,而且V实现了接口T。
V是指向数组的指针,T是一个切片,并且它们的元素类型兼容,而且T或V至少有一方是没有名字的。赋值后切片类型变量指向原数组;元素不被拷贝。
V是一个双向通道,而T是一个与V有兼容类型的通道,而且这两方也至少有一方要是匿名的。

If T is a struct type, either all fields of T must be exported, or the assignment must be in the same package in which T is declared. In other words, a struct value can be assigned to a struct variable only if every field of the struct may be legally assigned individually by the program.
如果T是一个结构体类型,要么它的所有成员变量都是公有的,要么赋值发生在声明T的包内。换句话说,如果想把一个结构体赋值给另一个结构体,就必须让该结构体的每一个成员变量都能被程序合法的赋值。

An untyped constant v is assignment compatible with type T if v can be represented accurately as a value of type T.
如果v是T类型,那么一个未命名的静态常量v才能与T类型赋值相容。

The predeclared identifier nil is assignment compatible with any pointer, function, slice, map, channel, or interface type and represents the zero value for that type.
预定义的标识符 nil 和 任意 pointer, funciton, slice, map, channel, interface 类型赋值相容,它代表那种类型的0值。

Any value may be assigned to the blank identifier.
任何值都可以为空白标识符赋值。

Comparison compatibility
比较兼容性

Except as noted, values of any type may be compared to other values of compatible static type. Values of integer, floating-point, and string type may be compared using the full range of comparison operators; booleans and complex values may be compared only for equality or inequality.
除非另有注明,所有类型的值都可以和相兼容的类型的静态变量进行比较。整形、浮点型和字符串类型的变量值可以用所有比较符来比较;布尔型和复数型的值只能比较它们是否相等。   

Values of composite type may be compared for equality or inequality using the == and != operators, with the following provisos:
合成类型的值可以通过 == 和 != 来比较是否相等,但要注意以下情况:

Arrays and structs may not be compared to anything.
数组和结构体可能不会任何数组进行比较。

A slice value may only be compared explicitly against nil. A slice value is equal to nil if it has been assigned the explicit value nil, if it is uninitialized, or if it has been assigned another slice value equal to nil?
切片只能和nil比较。如果一个切片被赋值为 nil或它没有初始化或它被赋值一个变量而且那个变量的值为nil,那么这个切片的就与 nil相等。

An interface value is equal to nil if it has been assigned the explicit value nil, if it is uninitialized, or if it has been assigned another interface value equal to nil.
接口的情况和切片相同。

For types that can be compared to nil, two values of the same type are equal if they both equal nil, unequal if one equals nil and one does not.
对所有能和 nil 比较,当且仅当有相同类型两个值都等nil时,它们才相等,如果一个为nil另一个不是,那么它们不相等。

Pointer values are equal if they point to the same location.
指针只要指向同一个位置就相等。

Function values are equal if they refer to the same function.
函数相等需要它们引用的是同一个函数。

Channel and map values are equal if they were created by the same call to make (§Making slices, maps, and channels). When comparing two values of channel type, the channel value types must be compatible but the channel direction is ignored.
通道和map一样,如果两个变量是被同一个make调用创建的,那么它们相等。在比较两个通道的值的时候,通道的值类型要求是兼容的,但是方向会被忽略。

Interface values may be compared if they have compatible static types. They will be equal only if they have the same dynamic type and the underlying values are equal.
接口可以和兼容的静态接口进行比较。如果它们有相同的动态类型并且它们的值是相等的。 阅读全文
类别:Golang 查看评论

转载于:https://my.oschina.net/zengsai/blog/4152

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值