This article will introduce you to the method of initializing the data structure in the GO language. GO provides 2 keywords to initialize the data structure. Let's take a look at how to use it~ I hope it will be helpful to everyone.
GO provides 2 keywords to initialize the data structure
But sometimes I will also initialize directly using curly braces, for example:
name := []string{"xiaomotong","bob","marry"}
In the above case, the value that needs to be filled in has been clearly defined during initialization. , you can use the above method, but in most cases when initializing at work, the data is still unknown, and most of the time the above two methods will still be used
make is provided by golang The keyword is mainly used to initialize built-in data structures. Custom data structures cannot be initialized. The data types that can be initialized by make are:
For example, when we initialize, we can write like this
strs := make([]string, 0, 10)myMap := make(map[string]string, 10)ch := make(chan struct{}, 10)
We look at the source code explanation of make and we can see that make does support the creation and initialization of the above 3 types
According to the explanation, we know that the type of the return value of make is consistent with the type of the filled in parameters. We fill in the slice type, then the return value is the slice type, fill in other types The corresponding return value is also the type we expect, no problem
So why do you need new to initialize the data structure with make?
Careful Fat Fish can see that as mentioned earlier, make is only suitable for initializing the built-in data structures provided by golang. For custom data structures, it is limited. At this time, it is still needed new took action
Using new to initialize the data structure can be written like this:
type T struct{ age int name string}func main(){ t := new(T) fmt.Println("t.name == ",t.name) fmt.Println("t.age == ",t.age)}
The program execution effect is as follows:
# go run main.got.name ==t.age == 0
As expected, it is The data structure was initialized for me, but the data are all zero values
Of course we can also write like this:
func main(){ a := new(int) fmt.Println("a == ",a) fmt.Println("*a == ",*a)}
The program execution effect is as follows:
# go run main.goa == 0xc420018078 *a == 0
According to the program running results, we know that new returns a pointer, and the corresponding value at the memory address pointed to by this pointer is a zero value of this type
golang source code says that the first parameter of new is a data type, not a value, and new The return value is a pointer that allocates zero-value memory according to the parameter type.
Therefore, the data structure printed by new we saw above is the zero value of the corresponding type.
Finally let’s sort out the difference between make and new to deepen the impression
make can only be used to initialize built-in data types
slice
map
Type, and new returns a pointer. It is of *Type type
Go video tutorial】
The above is the detailed content of Let's talk about how to initialize the data structure of GO (with code examples). For more information, please follow other related articles on the PHP Chinese website!