Go built-in functions (for reference types) the difference between new and make

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed. // Disgtinguish_new&make Project Main.go
/*
 Reference type  : slice channel map
 differences between new     and  make to create reference types
  New allocates a 0 value space of that size based on the size of the type, and then returns the first address of the space.   
Make is translated by the compiler into a concrete creation function, which allocates memory and initializes member variables, returning objects, not pointers,    
*/
Package Mainimport (. "FMT") func main () {var arr = []int{1, 2, 3, 4, 5, 6, 7, 8, 9}println (arr) var Slice1 = make ([]int, 5, ten)//Make create return is SL Ice Object Println (Slice1) Slice2: = new ([]int)//New creation Returns the slice pointer (*slice2) = Append ((*slice2), 1) Println (*SLICE2)/* Compile result 
  [1 2 3 4 5 6 7 8 9]   [0 0 0 0 0]   [1]*/var map1 = Make (Map[string]int, +) Println (MAP1) Map2: = new (Map[string]in T) (*MAP2) = map[string]int{} (*MAP2) ["Xuhe"] = 4Println ((*MAP2))/* Compile result map[]map[xuhe:4]*/}


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.