Golang type 类型定义与赋值

type A stringtype B []stringfunc 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 为什么没问题呢?如何去理解?


www说
浏览 1384回答 1
1回答

红糖糍粑

这个东西也算是 go 的坑吧,你也可以管它叫特性(/笑)对于切片类型,只要底层的类型相同,就认为是相同的类型,你可以把 B 换成 type B []A 试下
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python