type A string
type B []string
func main() {
one := "string"
two := []string{"string"}
var a A
var b B
b = two
a = one // cannot use one (type string) as type A in assignment
}
上面代码中,a=one
不正确很容易理解,以为A已经是一个新的类型了
那 b=two
为什么没问题呢?如何去理解?
这个东西也算是 go 的坑吧,你也可以管它叫特性(/笑)
对于切片类型,只要底层的类型相同,就认为是相同的类型,你可以把 B 换成
type B []A
试下