当前位置:网站首页>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
边栏推荐
- Machine learning theory (8): model integration ensemble learning
- Custom prompt box MessageBox in QT
- Analysez l'objet promise avec le noyau dur (Connaissez - vous les sept API communes obligatoires et les sept questions clés?)
- Promote QT default control to custom control
- Jeecg boot microservice architecture
- Quantexa CDI(场景决策智能)Syneo平台介绍
- STM32: LCD显示
- ctfshow-web362(SSTI)
- 串口调试工具cutecom和minicom
- Setting up keil environment of GD single chip microcomputer
猜你喜欢

Query the logistics update quantity according to the express order number

listener. log

【数学建模】—— 层次分析法(AHP)

os_ authent_ Prefix

Halo 开源项目学习(七):缓存机制

The vivado project corresponding to the board is generated by TCL script

【ACM】70. 爬楼梯

Spark performance optimization guide

【科普】CRC校验(一)什么是CRC校验?

Chondroitin sulfate in vitreous
随机推荐
[mathematical modeling] - analytic hierarchy process (AHP)
JD freefuck Jingdong HaoMao control panel background Command Execution Vulnerability
Daily network security certification test questions (April 18, 2022)
视频边框背景如何虚化,简单操作几步实现
STM32: LCD显示
实战业务优化方案总结---主目录---持续更新
Machine learning practice - naive Bayes
QT reading and writing XML files (including source code + comments)
os_ authent_ Prefix
Analysez l'objet promise avec le noyau dur (Connaissez - vous les sept API communes obligatoires et les sept questions clés?)
机器学习实战 -朴素贝叶斯
机器学习理论之(8):模型集成 Ensemble Learning
ctfshow-web362(SSTI)
On iptables
Daily CISSP certification common mistakes (April 13, 2022)
K210 serial communication
22 year flying Book manpower Kit
Notepad + + replaces tabs with spaces
QT error: no matching member function for call to ‘connect‘
ESP32 LVGL8. 1 - label (style 14)