Go Language Make & New

Source: Internet
Author: User
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)  
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.