当前位置:网站首页>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 :
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
channel
、map
、slice
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
版权声明
本文为[Ch3n]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231833257696.html
边栏推荐
- Solution to Chinese garbled code after reg file is imported into the registry
- 根据快递单号查询物流查询更新量
- 【ACM】509. Fibonacci number (DP Trilogy)
- 22年字节跳动飞书人力套件三面面经
- 多功能工具箱微信小程序源码
- CISSP certified daily knowledge points (April 18, 2022)
- QT notes on qmap container freeing memory
- CISSP certified daily knowledge points (April 12, 2022)
- Can filter
- 机器学习理论之(7):核函数 Kernels —— 一种帮助 SVM 实现非线性化决策边界的方式
猜你喜欢
机器学习理论之(7):核函数 Kernels —— 一种帮助 SVM 实现非线性化决策边界的方式
Notepad + + replaces tabs with spaces
Robocode tutorial 7 - Radar locking
CANopen STM32 transplantation
WIN1 remote "this may be due to credssp encryption Oracle correction" solution
Use stm32cube MX / stm32cube ide to generate FatFs code and operate SPI flash
Teach you to quickly rename folder names in a few simple steps
logstash 7. There is a time problem in X. the difference between @ timestamp and local time is 8 hours
【ACM】509. 斐波那契数(dp五部曲)
Differences between SSD hard disk SATA interface and m.2 interface (detailed summary)
随机推荐
Error reported when running tensorboard: valueerror: duplicate plugins for name projector, solution
Setting up keil environment of GD single chip microcomputer
Jeecg boot microservice architecture
Resolves the interface method that allows annotation requests to be written in postman
ESP32 LVGL8. 1 - anim animation (anim 16)
Can filter
Differences between SSD hard disk SATA interface and m.2 interface (detailed summary)
Configure iptables
STM32 learning record 0008 - GPIO things 1
回路-通路
Use Chenxi bookkeeping book to analyze the balance of revenue and expenditure of each account in a certain period of time
Daily network security certification test questions (April 13, 2022)
Quantexa CDI(场景决策智能)Syneo平台介绍
QT excel operation summary
Introduction to quantexa CDI syneo platform
硬核解析Promise對象(這七個必會的常用API和七個關鍵問題你都了解嗎?)
Matlab tips (6) comparison of seven filtering methods
22年字节跳动飞书人力套件三面面经
ctfshow-web362(SSTI)
Daily CISSP certification common mistakes (April 14, 2022)