Home > Backend Development > Golang > Let's talk about how to initialize the data structure of GO (with code examples)

Let's talk about how to initialize the data structure of GO (with code examples)

藏色散人
Release: 2023-01-16 20:29:47
forward
3436 people have browsed it

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.

How do you initialize the data structure during coding using golang?

GO provides 2 keywords to initialize the data structure

  • new
  • make

But sometimes I will also initialize directly using curly braces, for example:

name := []string{"xiaomotong","bob","marry"}
Copy after login

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

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:

  • slice slice
  • map hash table
  • channel channel

For example, when we initialize, we can write like this

  • Initialize a slice and fill in the len value, cap value. In addition to these two values, the underlying data structure of the slice also has a pointer, which points to an underlying array.
  • Initialize a map. The bottom layer of the map is a structure pointer pointing to an hmap. Inside the structure If you are interested in the specific members of xdm, you can read my historical article
  • Initialize a channel ch. This ch is a structure pointer pointing to an hchan. You can also read my historical article for details
strs := make([]string, 0, 10)myMap := make(map[string]string, 10)ch := make(chan struct{}, 10)
Copy after login

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

Lets talk about how to initialize the data structure of GO (with code examples)

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

new

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:

  • Customize a structure with 2 members, age and name
  • Use new to initialize T and return a pointer to a structure
type T struct{
    age int
    name string}func main(){

    t := new(T)
    fmt.Println("t.name == ",t.name)
    fmt.Println("t.age == ",t.age)}
Copy after login

The program execution effect is as follows:

# go run main.got.name ==t.age ==  0
Copy after login

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)}
Copy after login

The program execution effect is as follows:

# go run main.goa ==  0xc420018078
*a ==  0
Copy after login

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

Take a look at the new source code description

Lets talk about how to initialize the data structure of GO (with code examples)

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.

The difference between make and new

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

    • ##chan

      And new can assign any Type of data (including custom data types)

  • make returns a reference, which is of type

    Type, and new returns a pointer. It is of *Type type

【Related recommendation:

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!

Related labels:
source:learnku.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template