0
爲什麼下面的代碼不會驚慌? test
絕對是一個指針。與fmt.Println(people[0].Name)
而不是fmt.Println(test.Name)
它確實恐慌。刪除指針值不會恐慌
package main
import "fmt"
func main() {
type Person struct {
Id int
Name string
}
people := make(map[int]*Person)
people[1] = &Person{0, "Name"}
fmt.Println(people[0].Name)
test := people[0]
test.Name = "Name2"
fmt.Println(test.Name)
people[0].Name = "Name3"
fmt.Println(test.Name)
delete(people, 0)
fmt.Println(test.Name)
}
問題中的代碼與操場中的代碼不同。你問哪一個? – Art