自定义切片类型和基础类型之间的分配

Go assignment shows error for int but not for []int slice.

working code here

package main

import (
    "fmt"
)

type testType []int

func main() {
    var i testType
    var t []int
    t = i

    fmt.Println("Hello, playground", t, i)
}

However, if it is int type the compiler will surely show errors here:

cannot use i (type testType) as type int in assignment
package main

import (
    "fmt"
)

type testType int

func main() {
    var i testType
    var t int
    t = i

    fmt.Println("Hello, playground", t, i)
}

Why does it error out for the custom int type and not the custom []int type?

查看全部
du2986
du2986
2018/07/07 09:02
  • types
  • 点赞
  • 收藏
  • 回答
    私信
满意答案
查看全部

1个回复