我在代码中偶然发现了这种类型的别名:
type LightSource = struct {
R, G, B, L float32
X, Y, Z, A float32
//...
}
Run Code Online (Sandbox Code Playgroud)
我的问题是:使用类似别名来定义a的原因是什么struct,而不是这样做?
type LightSource struct {
R, G, B, L float32
//...etc
}
Run Code Online (Sandbox Code Playgroud)
我发现类型别名对于可读性很有用。例如,在测试中,您可以像这样比较 JSON 解码器的输出:
reflect.DeepEqual(r, map[string]interface{}{"a": map[string]interface{}{"b": 42.0}})
Run Code Online (Sandbox Code Playgroud)
但您可以使用类型别名来提高可读性:
type JsonObject = map[string]interface{}
...
reflect.DeepEqual(r, JsonObject{"a": JsonObject{"b": 42.0}})
Run Code Online (Sandbox Code Playgroud)
由于 DeepEqual 使用反射来比较类型(和值),因此将类型别名设置为类型定义(通过删除 = 字符)将导致 DeepEqual 失败。您可以在Go Playground中尝试一下
=,就是alias type。它不会创建新类型,只是创建相同类型的符号。=,就是type definition。它创造了一种新类型。alias type可以直接赋值,无需强制转换。new type需要强制转换,因为它是不同的类型。alias type可用于original type; 而方法type definition不能。type_alias_vs_new_test.go:
package main
import "testing"
type (
A = int // alias type
B int // new type
)
func TestTypeAliasVsNew(t *testing.T) {
var a A = 1 // A can actually be omitted,
println(a)
var b B = 2
println(b)
var ia int
ia = a // no need cast,
println(ia)
var ib int
ib = int(b) // need a cast,
println(ib)
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
644 次 |
| 最近记录: |