Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”

This article has participated in the “Digitalstar Project” and won a creative gift package to challenge the creative incentive money

XDM I am Nezha

How do you initialize data structures when coding with Golang

GO provides two keywords to initialize data structures

  • new
  • make

Sometimes, though, I use curly braces to initialize it directly, for example:

name := []string{"xiaomotong"."bob"."marry"}
Copy the code

In this case, the values to be filled are already specified during initialization, so you can use the above method. However, in most cases, the data is still unknown during initialization, and the above two methods will be used most of the time

make

Golang uses make to initialize built-in data structures. It does not initialize custom data structures. Make can initialize data types as follows:

  • Slice slice
  • Map the hash table
  • The channel tunnel

For example, we can write this when we initialize

  • Initialize a slice, fill in len, cap, and the underlying data structure of the slice with a pointer to an underlying array
  • Initialize a map. The underlying structure of the map is a pointer to an HMAP structure. XDM members of the structure can refer to my history article
  • Initialize a channel CH, which is a structure pointer to an Hchan, also check out my history article for details
strs := make([]string.0.10)
myMap := make(map[string]string.10)
ch := make(chan struct{}, 10)
Copy the code

If we look at the source code for make, we can see that make does support creation and initialization of these three types

We know that the return value of make is the same as the type of the parameter we filled in. If we fill in the slice type, the return value is the slice type. If we fill in the other types, the return value is the expected type

new

So why do we need new when we have make to initialize data structures?

Make is only good for initializing the built-in data structures provided by Golang. For custom data structures, there is no need for new data structures

To initialize a data structure with new, write:

  • Define a structure with two members, age and name
  • Initialize T using new to return a pointer to a structure
type T struct{
	age int
	name string
}

func main(a){
    
	t := new(T)
	fmt.Println("t.name == ",t.name)
	fmt.Println("t.age == ",t.age)

}
Copy the code

The execution effect of the program is as follows:

# go run main.go
t.name ==
t.age ==  0
Copy the code

Sure enough, I’m initializing the data structure, but the data is zero

Of course we can also write:

func main(a){

	a := new(int)
	fmt.Println("a == ",a)
	fmt.Println("*a == ",*a)

}
Copy the code

The execution effect of the program is as follows:

# go run main.go
a ==  0xc420018078
*a ==  0
Copy the code

We know that new returns a pointer to the memory address that corresponds to a zero value of that type

Take a look at the new source code description

The first argument to new is a data type, not a value, and the return value of new is a pointer that allocates zero-valued memory based on the parameter type

Therefore, the new data structure we saw above prints out the data of the corresponding type of zero

Difference between make and new

Finally, I will sort out the differences between make and new to deepen my impression

  • Make can only be used to initialize built-in data types

    • slice

    • map

    • chan

      While new can allocate data of any type (including custom data types)

  • Make returns a reference of Type Type, and new returns a pointer of Type *Type

Write a little bit every day

Welcome to like, follow and favorites

Friends, your support and encouragement, I insist on sharing, improve the quality of the power

All right, that’s it for this time

Technology is open, our mentality, should be more open. Embrace change, live in the sun, and strive to move forward.

I am Nezha, welcome to like, see you next time ~