Why can't I initialize a map with new() in Go?
问题 package main import "fmt" func main() { p := new(map[string]int) m := make(map[string]int) m["in m"] = 2 (*p)["in p"] = 1 fmt.Println(m) fmt.Println(*p) } The above code gives an error panic: assignment to entry in nil map . If I print *p before inserting pairs into it, the output is correct. It seems I just can't modify *p ? 回答1: Both new and make are used to allocate memory in a program, but they work differently. new(T, args) zeros memory and returns the memory address (a value of type *T