在 Go 中,我可以執行以下操作:func main() { var intSlice []interface{} intSlice = append(intSlice, "hello world") intSlice = append(intSlice, 1) for _, v := range intSlice { fmt.Println(v) // hello world // 1 }}由于切片是數組的深處,沒有給該數組指定特定類型,那么 Go 怎么知道這個數組的內存結構的布局?如果它是一個 [] 字符串,那么我知道對于每次迭代我都必須將當前地址與 4 相加以獲取下一個項目的地址,但是對于一個 interface{},Go 怎么知道該怎么做?我很困惑。對此的一種可能解釋是 interface{} 實際上是一個指針,因此 []interface{} 僅存儲指針,值 1 或“hello world”存儲在切片之外的某個位置。我是對的嗎?
Go 中的 []interface{} 是如何實現的?
慕田峪9158850
2022-10-17 19:21:53