1. Introduction to new function
In Go, the new function is used to dynamically allocate memory, returning a pointer to the newly allocated zero value. Its syntax is as follows:
func new(Type) *Type
Where Type represents the type of memory to be allocated, and the new function returns a pointer to the newly allocated zero value of type Type. But it should be noted that the new function only allocates memory and returns a pointer to the newly allocated zero value without initializing that memory.
The so-called zero value refers to the default value automatically assigned by variables in Go language when declared. For basic types, their zero values are as follows:
- Boolean type: false
- Integer: 0
- Floating point type: 0.0
- Plural type: 0 + 0i
- String: "" (empty string)
- Pointer: nil
- Interface: nil
- Slices, maps and channels: nil
Therefore, the pointer returned by the new function points to the newly allocated zero value, but does not initialize it to a non-zero value. If you need to initialize memory to a non-zero value, you can use the structure literal or explicitly assign it.
2. Example
(1) Example 1:
package main import ( "fmt" ) func main() { var p *int p = new(int) (*p) //0 }
The above code first declares a pointer p to an integer, and then uses the new function to assign a new integer to assign its address to p. Since the pointer returned by the new function points to the newly allocated zero value, the value of p is 0.
(2) Example 2:
In actual programming, the new function is usually used with the structure type to dynamically allocate memory for the structure. For example, the following code defines a struct type named Person and allocates memory to it using the new function:
package main import "fmt" type Person struct { name string age int sex int } func main() { // Allocation of memory using the new function, but it will not be initialized to a non-zero value p := new(Person) (p) // Output: &{ 0 0} // Initialize using structure literal p2 := &Person{name: "Tom", age: 18, sex: 1} (p2) // Output: &{Tom 18 1} // explicitly assign values to fields p3 := new(Person) = "Jerry" = 20 = 0 (p3) // Output: &{Jerry 20 0}}
In the above code, a new Person structure is assigned using the new function, but it is not initialized to a non-zero value, so the output is "empty string 0 0". Next, initialize it to a non-zero value using the structure literal or explicitly assign it.
Note 1: p3 := new(Person) returns a pointer to the zero value of the newly assigned Person type object. According to our understanding of pointer syntax, if the assignment is displayed based on p3, you need to use the following syntax for assignment:
(*p3).name = "Jerry" (*p3).age = 20 (*p3).sex = 0
When we assign values to pointer-type structure objects, we rarely carry *, which is a simplification made by Go pointer syntax sugar for us.
3. Summary
The expression new(T) will create an anonymous variable of type T. What is done is to allocate and clear a piece of memory space for the new value of type T, and then return the address of this piece of memory space as the result. The result is the pointer value to the new T-type value, and the returned pointer type is *T.
The memory space created by new is located on the heap, and the default value of the space is the data type default value. For example: new(int) then *p is 0, new(bool) then *p is false. If you need to initialize memory to a non-zero value, you can use the structure literal or explicitly assign it.
We just need to use the new() function, without worrying about the life cycle of its memory or how to delete it, because the memory management system of Go will help us manage everything.
This is the introduction to this article about the detailed explanation of the usage of new functions in Go language learning. For more related content of new functions in Go language, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!