web123456

golang: The difference between make and new

golang There are twobuilt-in functionThe functions that can be used to allocate memory on the heap are mak() and new(), which will be briefly described in this article, along with the differences between these two functions, and the circumstances in which each of these functions is used.

Let's start by looking at the definitions of the two built-in functions in golang.

new():

  1. func new(Type) *Type
  2. The new built-in function allocates memory. The first argument is a type, not a value, and the value returned is a pointer to a newly allocated zero value of that type.

make();

  1. func make(t Type, size ...IntegerType) Type
  2. The make built-in function allocates and initializes an object of type slice, map, or chan (only). Like new, the first argument is a type, not a value. Unlike new, make's return type is the same as the type of its argument, not a pointer to it.

From the definitions of the two functions above, we can see two distinct differences.

Difference 1: the number of parameters received is different

As you can see from the definition of the new() function, it takes only one argument -- Type, which is the type of memory to be allocated.

And as you can see from the definition of the make() function, it can take multiple arguments, the first of which is also Type, and the rest of which are optional integer type arguments.

The make() function can allocate memory for slice types. When the make() function allocates memory for slice types, optional integer parameters can be used to specify the length and capacity of the slice, as shown in the following example:

  1. package main
  2. import (
  3. "fmt"
  4. )
  5. func main() {
  6. my_slice := make([]int, 5, 10)
  7. (my_slice)
  8. }

Running the above program will give you the following output:

[0 0 0 0 0]

As you can see, golang assigns us a slice of length 5.

Difference 2: different return types

The new() function returns a pointer to the type of the received argument.

The make() function returns the same type as the first argument it receives.

Let's verify this with an example below:

  1. package main
  2. import (
  3. "fmt"
  4. )
  5. func main() {
  6. new_int := new(int)
  7. ("new_int type: %T\n", new_int)
  8. make_slice := make([]int, 5)
  9. ("make_slice: %T\n", make_slice)
  10. }

Run the above program and the output is as follows:

  1. new_int type: *int
  2. make_slice: []int

As you can see, the type of new_int is a pointer to the type int; and make_slice is a slice containing the type int.

Difference three: different application scenarios

The make() function is specifically designed to allocate and initialize memory for slice, map, and chan, while new() is more of a general purposememory allocationtool that can allocate memory for other types.

As mentioned above, new() is more of a general-purpose memory allocation tool, but it can't allocate memory for reference types like slice, map, or chan. This is because new() simply allocates memory and does a zero value on the memory depending on the type.

Reference types such as slice, map, and chan create a value called header, which is actually a data structure that contains a pointer to the underlying data structure, and depending on the reference type, different fields to manage the underlying data structure.

Taking the slice type as an example, when a value of type slice is created, a lightweight data structure is created that contains three fields: pointer, length, and capacity.

  • A pointer is a pointer to an underlying array.
  • length represents the length of the slice.
  • capacity represents the capacity of the slice

You can see why you can't use new() to create reference types like slice, map, and chan. If you use new() to create a slice, then the pointer in the header you create will be initialized to nil as a 0-value, and the length and capacity will also be initialized to 0, which is obviously incorrect.

summarize

new() and make() are both functions used to allocate memory in golang. This article describes the three main differences between make() and new():

  • The number of arguments is different: new() takes only one argument, while make() can take multiple arguments.
  • The return types are different: new() returns a pointer, whereas make() returns the same type as the first argument it receives
  • The application scenarios are different: make() is used exclusively to allocate and initialize reference types like slice, map, and chan, while new() is used to allocate memory for other types.

As always, if you have any questions about the content in the article, or find any errors in the article, you can let me know by leaving a comment; if you like my articles, feel free to follow my WeChat public number Tech For Geek.