A detailed explanation of the difference between new and make in Go

Updated on technology 2024-08-06
3 answers
  1. Anonymous users2024-02-15

    1. The main features of new.

    First of all, new is a built-in function, and the definition is very simple:

    func new(type) *type

    The built-in function new is used to allocate memory, the first parameter is a type, not a value, and the return value is a pointer to the zero value of the new allocation type.

    Implement a feature like new:

    func newint() int {

    var i int

    return &i

    someint := newint()

    The function functions exactly the same as someint := new(int). When defining a function that starts with new, a pointer to the type should also be returned by convention.

    2. The main features of make.

    make is also a built-in function, which defines one more parameter than new, and the return value is different:

    func make(type, size integertype) type

    The built-in function make is used to allocate memory and initialize an object for the slice, map, or chan type (note: it can only be used on these three types), similar to new, the first parameter is also a type instead of a value, unlike new, make returns a reference to the type instead of a pointer, and the return value also depends on the specific type passed in, as described below:

    slice: The second parameter size specifies the length, and the capacity and length are the same.

    You can pass in a third parameter to specify a different capacity value, but it must not be smaller than the length value.

    For example, make(int, 0, 10).

    map: Initializes the allocated memory according to the size size, but the allocated map length is 0, if the size is ignored, a small size of memory will be allocated when the memory is initially allocated.

    channel: The pipeline buffer is initialized based on the buffer capacity. If the capacity is 0 or the capacity is ignored, the pipeline does not have a buffer.

    3. Summary. The role of new is to initialize a pointer to a type (*t), and the role of make is to initialize and return a reference (t) for a slice, map, or chan.

  2. Anonymous users2024-02-14

    new: is a built-in function used to allocate memory, different from C++, it does not initialize memory, but just zeros it, which is equivalent to, new(x) will allocate zeroed storage for the new project of x, and return its address, where the first parameter is the type, the return value is the pointer of the type, and its value is initialized as '0', for different data types, the meaning of the 0 value is also different, such as int initialization to 0, bool initialization to false, etc.

    make: is a built-in function of golang, only used to allocate and initialize objects of slice, map and channel types, all three types are structures, the return value is type instead of pointer, for example, slice is a ternary descriptor, containing a pointer to the data (in the array), length and capacity, before these items are initialized, slice is nil, for these three, make initializes these internal data structures, And have ready the values that are available.

    It is important to note that make is only used for map, slice, and channel, and does not return pointers, so if you want to get an explicit pointer, use new for allocation, or explicitly use the address of a variable.

  3. Anonymous users2024-02-13

    Make is used for memory allocation for built-in types (map, slice, and channel). new is used for various types of memory allocations.

    The built-in function new essentially functions the same as the function of the same name in other languages: new(t) allocates the memory space of type t filled with zero values, and returns its address, which is the value of type *t. In go's terminology, it returns a pointer to the zero value of the newly assigned type t.

    One thing is very important: new returns the pointer.

    The built-in function make(t, args) has a different function than new(t) in that make can only create slice, map, and channel, and returns a t type with an initial value (non-zero) instead of *t.

    Essentially, the reason why these three types differ is that references to data structures must be initialized before they can be used. For example, a slice is a three-item descriptor that contains a pointer, length, and capacity to the data (the internal array); Until these items are initialized, Slice is nil. For slice, map, and channel, make initializes the internal data structure and populates the appropriate values.

    make returns a (non-zero) value after initialization.

    make is the method of reference type initialization.

Related questions
8 answers2024-08-06

What is Python? Python is a powerful high-level programming language, mainly used in scientific and engineering computing, is an efficient programming language with a concise and powerful layout, suitable for novices as well as professionals to learn. >>>More

11 answers2024-08-06

Valid variable names for the C language:

First, it can only contain numbers, letters, and underscores. >>>More

21 answers2024-08-06

The third-generation Vios is the most capable of fully interpreting the characteristics of keen look, precisely because its body is the tightest of the three, and it can accumulate dynamic momentum in it

6 answers2024-08-06

It seems that you don't know much about structs and struct pointers, the data array is a struct array that you define, it consists of two knot body elements, and each struct element contains two members x and y, the first element is 1 and 10, the second element is 2 and 20, the struct pointer p you defined starts to point to the first element of the array data, p points to the second element of the data array, and the pointer p can manipulate the two members x and y >>>More

17 answers2024-08-06

include sets the insertion point.

include character processing. >>>More