【发布时间】:2018-12-15 17:20:00
【问题描述】:
Go 分配显示int 的错误,但[]int 切片却没有。
工作代码here
package main
import (
"fmt"
)
type testType []int
func main() {
var i testType
var t []int
t = i
fmt.Println("Hello, playground", t, i)
}
但是,如果是int类型,编译器肯定会报错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)
}
为什么自定义 int 类型而不是自定义 []int 类型会出错?
【问题讨论】:
-
所以你想要它抱怨?为什么?