我是 Go 的新手,我来自 Ruby。所以...
我可以构建包含不同类型的数组吗
[1, 2, "apple", true]
Run Code Online (Sandbox Code Playgroud)
对不起,如果这是愚蠢的问题。
谢谢。
abh*_*ink 12
您可以通过制作interface{}类型切片来做到这一点。例如:
func main() {
arr := []interface{}{1, 2, "apple", true}
fmt.Println(arr)
// however, now you need to use type assertion access elements
i := arr[0].(int)
fmt.Printf("i: %d, i type: %T\n", i, i)
s := arr[2].(string)
fmt.Printf("b: %s, i type: %T\n", s, s)
}
Run Code Online (Sandbox Code Playgroud)
在此处阅读更多相关信息。