This is a creation in Article, where the information may have evolved or changed.
Both make and new are the ways in which the Go language allocates variable memory, where makes primarily targets memory allocations for slice, map and Chan variable types, and the initialization of corresponding internal structures, while new can request any type of variable memory, but when it gets memory, it assigns a value of 0 to slice, The application of map and Chan type variables is not very significant. The following is a simple analysis of the differences between the two memory allocations based on the actual example.
assigning Sclie
var s0 []intappend(s0, 10new([]intappend(*s1, 10make([]int, 0append(s2, 10make([]int, 10append(s3, 10)fmt.Println(&s3, s3)
Assign Map
varM0Map[string]intFmt. Println (&M0, M0) M1: =New(Map[string]int) FMT. PRINTLN (M1, *m1) m2: = Make(Map[string]int,0) m2["Key1"] = -Fmt. Println (&m2, m2) m3: = Make(Map[string]int,Ten) m3["Key1"] = -Fmt. Println (&m3, m3) fmt. Println (Len(m2))
Assign Chan
var C0 chan int = make (chan int ) go Func (c chan int ) {c <- } (C0) Res: = <-c0fmt. PRINTLN (res) x: = new (S) fmt. Printf ( "% #v" , x)