The difference between new () and make () in the Go language

Source: Internet
Author: User
Tags builtin
This is a creation in Article, where the information may have evolved or changed.

http://se77en.cc/2014/04/25/the-difference-between-function-new-and-make-in-golang/

Overview

The Go language new and make has always been the novice more easily confused things, I look very similar. But explaining the difference between the two is also very easy.

Key features of new

The first new is the built-in function, you can see it from http://golang.org/pkg/builtin/#new here, and its definition is simple:

1
New (type) *type

The official documentation describes it as:

The built-in function new is used to allocate memory, its first parameter is a type, not a value, and its return value is a pointer to the new assigned type 0 value

Based on this description, we can implement a similar new function ourselves:

123456
func newint () *int {  varint  return &i}someint: = Newint ()

Our function is identical to the function someInt := new(int) . So when we define our own functions that start with new, we should also return pointers to types for conventions.

Key features of Make

makeis also a built-in function, you can see it from http://golang.org/pkg/builtin/#make here, its definition is new more than one parameter, the return value is also different:

1
Func make (typetype

The official documentation describes it as:

The built-in function is make used to slice map allocate memory for, or type, chan and initialize an object ( Note : Only available on these three types), new like, the first parameter is also a type instead of a value, new unlike the makereturns a reference to a type other than a pointer, and the return value depends on the type that is passed in, as described below:

1234567
The size specifies its length, and its capacity and length are the same. You can pass in the third parameter to specify a different capacity value, but must not be smaller than the length value. such as make ([]int0size0size0 or ignore capacity, pipeline is no buffer

Summarize

newThe function of initializing a pointer to a type ( *T ) make is to initialize slice map chan and return a reference ( T ).


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.