当前位置:网站首页>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 slice、map、channel 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 slice、chan、map 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 :
makefunction :- 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
channel、map、sliceThree types of . - Have a unique advantage ,make Function will be used for three types of internal data structures ( length 、 Capacity, etc ) assignment .
newfunction :- 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
版权声明
本文为[Ch3n]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231833257696.html
边栏推荐
- Interpretation and compilation of JVM
- ctfshow-web362(SSTI)
- 【ACM】70. climb stairs
- Serial port debugging tools cutecom and minicom
- Test post and login function
- Daily network security certification test questions (April 18, 2022)
- ESP32 LVGL8. 1 - event (event 17)
- 深入理解 Golang 中的 new 和 make 是什么, 差异在哪?
- Use Chenxi bookkeeping book to analyze the balance of revenue and expenditure of each account in a certain period of time
- 22年字节跳动飞书人力套件三面面经
猜你喜欢

MATLAB小技巧(6)七种滤波方法比较

【ACM】70. 爬楼梯

Halo open source project learning (VII): caching mechanism

os_authent_prefix

Dynamically add default fusing rules to feign client based on sentinel + Nacos
![Resolve the error Max virtual memory areas VM max_ map_ count [65530] is too low, increase to at least [262144]](/img/5f/a80951777a0473fcaa685cd6a8e5dd.png)
Resolve the error Max virtual memory areas VM max_ map_ count [65530] is too low, increase to at least [262144]

STM32: LCD display

Custom prompt box MessageBox in QT

iptables初探

ESP32 LVGL8. 1 - label (style 14)
随机推荐
CANopen STM32 transplantation
QT notes on qmap container freeing memory
Daily network security certification test questions (April 14, 2022)
Nodejs installation
The vivado project corresponding to the board is generated by TCL script
22 year flying Book manpower Kit
listener. log
iptables -L执行缓慢
K210 serial communication
ctfshow-web362(SSTI)
K210串口通信
【ACM】509. Fibonacci number (DP Trilogy)
Halo open source project learning (VII): caching mechanism
On iptables
Log4j2 cross thread print traceid
Kettle paoding jieniu Chapter 17 text file output
According to the result set queried by SQL statement, it is encapsulated as JSON
ctfshow-web361(SSTI)
MATLAB从入门到精通(二)
硬核解析Promise对象(这七个必会的常用API和七个关键问题你都了解吗?)