Go new() Function: Memory Allocation Guide
In the Go language, the new function is used to allocate memory and return a pointer to that memory. When using the new function, you need to pass the type of memory to be allocated as a parameter. For example:
package main
import "fmt"
func main() {
var i *int
i = new(int)
*i = 42
fmt.Println(*i) // 输出:42
}
In the example above, first declare a pointer i pointing to an int type, then allocate memory space for an int type using new(int), assign its address to i, finally assign a value of 42 to this memory space using *i, and print it out.