Learn more about golang (4)-New and make

Source: Internet
Author: User

The built-in functions new and make in the go language are two primitives for memory allocation (Allocation primitives ). For beginners, the difference between the two is also quite confusing. In short, new only allocates memory. Make is used for slice, map, and channel initialization.

 

1. New

This is a built-in function used to allocate memory, but unlike C ++, it does not initialize memory, but just sets it to zero. That is to say, new (t) will allocate zero storage for T-type new projects and return its address, a value of type * t. In go terminology, it returns a pointer pointing to the newly allocated type of T, and the value of the content pointed to by this pointer is zero value ). Note that the pointer is not zero.

Objects in the go language do not have constructors in C ++. If C is used to describe objects, the new in the go language is roughly equivalent:

T * t = (T *) malloc (sizeof (t ))

Memset (T, 0, sizeof (t ))

In fact, the above description may not be very accurate, and it may be more accurate with * t = zerovalue. For different data types, the meaning of zero value is completely different. For example, for the bool type, the zero value is false; the zero value of int is 0; the zero value of string is a Null String:

B: = new (bool)

FMT. println (* B)

I: = new (INT)

FMT. println (* I)

S: = new (string)

FMT. println (* s)

Output:

False

0

 

Note that there is an empty string at the end.

 

2. Initialization

In many cases, zero value is not a good idea. We need to perform initialization. Consider the following struct:

Type rect struct {

X, Y float64

Width, height float64

}

Zero-value rect is not very useful. We can initialize it in the following way:

Rect3: = & rect {0, 0,100,200}

Rect4: = & rect {width: 100, height: 200}

 

Again, the Go language does not have any constructor in C ++. The object is generally created by a global creation function:

Func newrect (X, Y, width, height float64) * rect {

Return & rect {X, Y, width, height}

}

Note that, unlike C/C ++, the address for returning a local variable is absolutely no problem in the go language. The storage of variable associations still exists after the function returns.

More directly, in the go language, if a local variable is still used after the function returns, the variable will be allocated from heap instead of stack. For more information, see How do I know whether a variable is allocated on the heap or the stack ?.

 

3. Make

The built-in make (T, argS) function is used differently from the new (t) function. It is only used to create slice, map, and channel, and returns an initialized (instead of set to zero) value of type T (rather than * t ). The difference is that the three types reference the data structure that must be initialized before use. For example, slice is a ternary descriptor that contains a pointer to data (in the array), length, and capacity. Before these items are initialized, slice is nil. For slice, map, and channel, make initializes these internal data structures and prepares available values.

 

For example,

Make ([] int, 10,100)

Allocate an array with 100 int values, and create an slice structure with a length of 10 and a capacity of 100. This slice references an array containing the first 10 elements. Correspondingly, new ([] INT) returns a pointer to the newly allocated, zero-placed slice struct, that is, the pointer to the nil-valued slice.

 

VaR p * [] Int = new ([] INT) // allocates slice structure; * P = nil; rarely useful

VaR V [] Int = make ([] int, 100) // The Slice V now refers to a new array of 100 ints

 

// Unnecessarily complex

VaR p * [] Int = new ([] INT)

* P = make ([] int, 100,100)

 

// Idiomatic: a habit

V: = make ([] int, 100)

Remember that make is only used for map, slice, and channel, and no pointer is returned. To obtain an explicit pointer, use new for allocation, or explicitly use the address of a variable.

 

References

Aggressive go


YY brother
Source: http://www.cnblogs.com/hustcat/
The copyright of this article is shared by the author and the blog Park. You are welcome to repost this article. However, you must retain this statement without the author's consent and provide a clear link to the original article on the article page. Otherwise, you will be held legally liable.

Learn more about golang (4)-New and make

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.