Introduction
In Go,make
It is a built-in function for creating reference types such as slices, maps, and channels.make
The main function is to allocate memory and initialize these reference types.
usage:
1. Create slice:
slice := make([]int, 5) // Create a slice containing 5 integers
make
Accept two parameters, the first is type and the second is length. For slices, an optional capacity parameter can also be provided:
slice := make([]int, 5, 10) // Create a slice with a length of 5 and a capacity of 10
2. Create a map:
myMap := make(map[string]int) // Create a string to integer mapping
3. Create a channel:
ch := make(chan int) // Create an integer type channel
Notes:
-
make
It can only be used for reference type data structures, and cannot be used for the creation of value types (such as structures). -
make
Returns the initialized reference type instance, not a pointer. - For slices and mappings,
make
In addition to allocating memory, internal data structures are also initialized to ensure that they can be used directly. - For the channel,
make
An unbuffered channel will be created and returned.
Overall,make
Mainly used to create reference types and initialize them to ensure that they can be used correctly.
New Introduction
In Go,new
is a built-in function for allocating memory for a value type and returning a pointer to a newly allocated zero-value instance.new
Mainly used to create instances of value types, such as structures. The following isnew
Basic usage and precautions:
usage:
1. Create a value type instance:
ptr := new(int) // Create an integer type pointer to the newly allocated zero-valued integer
2. Create a structure instance:
type MyStruct struct { Field1 int Field2 string } instance := new(MyStruct) // Create a pointer of type MyStruct to the newly allocated zero-value structure
Notes:
-
new
Returns a pointer to a zero-value instance of newly allocated memory. - For value types,
new
The allocated memory will be initialized to a zero value. -
new
Accepts a parameter, that is, the type to allocate memory, and returns a pointer to the zero value of that type. -
new
Not applicable to reference types (such as slices, maps, and channels), they can only be used for the creation of value types. -
new
The allocated memory will not be cleaned up and the programmer needs to be responsible for freeing it.
Overall,new
Mainly used to allocate memory for value types and return a pointer to a newly allocated zero-value instance. When using it in actual use, choose to use it as needednew
Or declare and initialize directly.
The similarities and similarities between make and new
make
andnew
They are all built-in functions for memory allocation in Go, but their uses and behaviors have some key differences.
Similarities:
- Memory allocation:Both are used to allocate memory on the heap.
Differences:
1. Applicable types:
-
make
:make: only forslice
、map
as well aschannel
Initialization.
slice := make([]int, 5)
-
new
:Used to create instances of value types, such as structures. Returns a pointer to the newly allocated zero-value instance.
ptr := new(int)
2. Return type:
make
:Returns the initialized reference type instance, not a pointer.new
:Returns a pointer to the newly allocated zero-value instance.
3. Parameters:
-
make
:Accept parameters such as type and length to initialize the internal data structure of the reference type.
slice := make([]int, 5)
-
new
:Only accept type parameters, returning a pointer to the zero value of that type.
ptr := new(int)
4. Initialize:
make
:In addition to allocating memory, internal data structures of reference types are also initialized.new
:The allocated memory will be initialized to a zero value.
Summarize:
- use
make
Mainly used to initialize reference types to ensure that they can be used directly. - use
new
Mainly used to allocate memory for value types, returning a pointer to a newly allocated zero-value instance.
Choose to usemake
stillnew
Depends on the type of data structure you are creating and the initialization requirements.
The above is a detailed explanation of the similarities, similarities and usage of make and new in golang. For more information on the similarities, similarities and usage of golang make and new, please pay attention to my other related articles!