当前位置:网站首页>Deeply understand what new and make in golang are and what are the differences?

Deeply understand what new and make in golang are and what are the differences?

2022-04-23 18:36:00 Ch3n

stay Go In language , There are two similar built-in functions , Namely new and make Method , Its main purpose is to allocate corresponding types of memory space .

it seems new and make All allocate memory , What's the difference between them ? This detail has also become a lot of Go One of the interview questions for language Engineers , It's worth seeing .
In this article, we will answer this question in the future .

Basic characteristics

make

stay Go In language , Built in functions make Support only slicemapchannel Memory creation of three data types , Its return value is the type itself created , Instead of a new pointer reference .

The function signature is as follows :

func make(t Type, size ...IntegerType) Type

Specific use examples :


func main() {
    
 v1 := make([]int, 1, 5)
 v2 := make(map[int]bool, 5)
 v3 := make(chan int, 1)
    
 fmt.Println(v1, v2, v3)
}

In the code , We call... For each of the three types make Function is initialized . You will find that some input parameters have multiple length specifications , Some have not .

The main difference is length (len) and Capacity (cap) The specified , There are some types that have no capacity , So naturally it is impossible to specify .

Output results :

[0] map[] 0xc000044070

There is one detail to note , call make Function de initialization section (slice) The type of , Will have a zero value , It is necessary to clarify whether it is necessary .
I've seen a lot of little friends trample on this .

new

stay Go In language , Built in functions new You can create and initialize memory for types . Its return value is a pointer reference to the type created , And make Functions differ in substance and detail .

The function signature is as follows :

func new(Type) *Type

Specific use examples :

type T struct {
    
 Name string
}

func main() {
    
 v := new(T)
 v.Name = " Fried fish "
}

From the effect of the above example , Is it similar ? In fact, it is the same as the following way :

func main() {
    
 v := T{
    }
 v.Name = " Fried fish "
}

The output results are :

&{
    Name: Fried fish }

Actually new Functions are rare in daily engineering code , Because he can be replaced .
Generally, it will be used directly and quickly T{} To initialize , Because the conventional structure will have the literal attribute of the structure :

func NewT() *T {
    
 return &T{
    Name: " Fried fish "}
}

This initialization method is more convenient .

What's the difference

Maybe some little friends will be a little confused , Namely new Functions can also be initialized make Three types of :

v1 := new(chan bool)
v2 := new(map[string]struct{
    })

that make Difference of function , What are the advantages ?
Essentially make Function at initialization , Will the initialization slicechanmap Type's internal data structure ,new Function does not .
for example : stay map Type in the , Reasonable length (len) And capacity (cap) It can improve efficiency and reduce overhead .
Further difference :

  • make function :
    • Be able to allocate and initialize the memory space and structure required by the type , Returns the reference type itself .
    • It has limitations in the scope of use , Support only channelmapslice Three types of .
    • Have a unique advantage ,make Function will be used for three types of internal data structures ( length 、 Capacity, etc ) assignment .
  • new function :
    • Ability to allocate memory space required by type , Return pointer reference ( Pointer to memory ).
    • Can be replaced by , It can be initialized quickly through literal value .

source I have fried fish in my head official account

Original article

版权声明
本文为[Ch3n]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231833257696.html