广州网站设计实力乐云seo,洛阳网站建设公司,营销网站建设选择,多多进宝cms网站建设Go#xff08;或 Golang#xff09;是一种现代、静态类型、编译型的编程语言#xff0c;专为构建可扩展、并发和高效的软件而设计。它提供了各种内置的函数和特性#xff0c;帮助开发人员编写简洁高效的代码。其中包括 new() 和 make() 函数#xff0c;这两个函数乍看起来… 
Go或 Golang是一种现代、静态类型、编译型的编程语言专为构建可扩展、并发和高效的软件而设计。它提供了各种内置的函数和特性帮助开发人员编写简洁高效的代码。其中包括 new() 和 make() 函数这两个函数乍看起来可能相似但在 Go 中用于不同的目的对于内存分配和数据初始化至关重要。 
在本博客文章中我们将探讨 new() 和 make() 函数之间的区别了解何时以及如何有效地使用它们。 
new() 和 make() 函数 
new() 和 make() 都是 Go 中的内置函数用于分配内存。然而它们用于不同的数据类型和场景 
new() 函数 
new() 用于为值类型例如整数、浮点数、结构体分配内存并返回指向新分配的零值的指针。它接受一个参数即类型并返回该类型的指针。 
make() 函数 
make() 用于创建和初始化切片、映射和通道这些都是 Go 中的引用类型。它根据类型的不同接受两个或三个参数并返回一个已初始化非零值的值可以立即使用。 
理解 new() 函数 
new() 函数的语法非常简单如下所示 
func new(Type) *Type这里的 Type 表示我们想要为其分配内存的值的类型。让我们看一个如何使用 new() 的示例。 
在这个示例中我们使用 new() 创建了 Person 结构体的一个新实例然后使用指针分配值给其字段。 
package mainimport fmttype Person struct {Name stringAge  int
}func main() {// Using new() to allocate memory for a Person structp : new(Person)fmt.Printf(%T\n, p)// Accessing struct fields using the pointerp.Name  Alicep.Age  30// Displaying the valuesfmt.Println(Name:, p.Name)fmt.Println(Age:, p.Age)
}这个程序将产生如下所示的输出。 go run main.go
*main.Person
Name: Alice
Age: 30理解 make() 函数 
make() 函数的语法取决于它所用于的类型。 
对于切片Slices 
func make([]Type, len, cap) []TypeType切片将保存的元素类型。len切片的初始长度。cap切片的容量这是可选的并用于指定底层数组的容量。如果未提供它默认与长度相同。 
使用 make() 创建切片的示例 
package mainimport fmtfunc main() {// Using make() to create a slice of integersnumbers : make([]int, 5, 10)// Displaying the slices length, capacity, and valuesfmt.Println(Length:, len(numbers))fmt.Println(Capacity:, cap(numbers))fmt.Println(Values:, numbers)// Using make() to create a slice of integersnumbersWithoutOptional : make([]int, 5)// Displaying the slices length, capacity, and valuesfmt.Println(Length:, len(numbersWithoutOptional))fmt.Println(Capacity:, cap(numbersWithoutOptional))fmt.Println(Values:, numbersWithoutOptional)
}此程序将产生如下输出。 go run main.go
Length: 5
Capacity: 10
Values: [0 0 0 0 0]
Length: 5
Capacity: 5
Values: [0 0 0 0 0]对于映射Maps 
func make(map[KeyType]ValueType, initialCapacity int) map[KeyType]ValueTypeKeyType映射中键的类型。ValueType与键关联的值的类型。initialCapacity映射的初始容量。这是可选的但当预先知道元素数量时可以用于优化性能。 
使用 make() 创建映射的示例 
package mainimport fmtfunc main() {// Using make() to create a map of string keys and int valuesscores : make(map[string]int)// Adding values to the mapscores[Alice]  95scores[Bob]  87// Displaying the mapfmt.Println(Scores:, scores)
}go run main.go
Scores: map[Alice:95 Bob:87]对于通道Channels 
func make(chan Type, capacity int) chan TypeType可以通过通道发送和接收的值的类型。capacity通道的缓冲区大小。如果设置为0通道是无缓冲的。 
使用 make() 创建通道的示例 
package mainimport (fmttime
)func main() {// Using make() to create an unbuffered channel of integersch : make(chan int)// Sending data into the channel using a goroutinego func() {for i : 1; i  5; i {ch - itime.Sleep(time.Second) // Simulating some work before sending the next value}close(ch)}()// Receiving data from the channelfor num : range ch {fmt.Println(Received:, num)}
}go run main.go
Received: 1
Received: 2
Received: 3
Received: 4
Received: 5结论 
在本博客文章中我们解开了 Go 中的 new() 和 make() 函数的谜团并解释了它们的区别和用途。总结一下 
使用 new() 为值类型分配内存并获取指向零值的指针。使用 make() 创建和初始化切片、映射和通道引用类型并指定它们的类型和初始容量。 
理解 new() 和 make() 之间的区别对于在 Go 中进行高效的内存分配和数据初始化至关重要。正确使用这些函数将在您的 Golang 项目中产生更干净和更优化的代码。愿您编程愉快 文章转载自: http://www.morning.tscsd.cn.gov.cn.tscsd.cn http://www.morning.dwkfx.cn.gov.cn.dwkfx.cn http://www.morning.wpcfh.cn.gov.cn.wpcfh.cn http://www.morning.zmpqh.cn.gov.cn.zmpqh.cn http://www.morning.nxbsq.cn.gov.cn.nxbsq.cn http://www.morning.cmhkt.cn.gov.cn.cmhkt.cn http://www.morning.qblcm.cn.gov.cn.qblcm.cn http://www.morning.nzsdr.cn.gov.cn.nzsdr.cn http://www.morning.rxzcl.cn.gov.cn.rxzcl.cn http://www.morning.tklqs.cn.gov.cn.tklqs.cn http://www.morning.drytb.cn.gov.cn.drytb.cn http://www.morning.mwqbp.cn.gov.cn.mwqbp.cn http://www.morning.bpmtl.cn.gov.cn.bpmtl.cn http://www.morning.nmnhs.cn.gov.cn.nmnhs.cn http://www.morning.tbhlc.cn.gov.cn.tbhlc.cn http://www.morning.nrtpb.cn.gov.cn.nrtpb.cn http://www.morning.qnbzs.cn.gov.cn.qnbzs.cn http://www.morning.zrlms.cn.gov.cn.zrlms.cn http://www.morning.mwcqz.cn.gov.cn.mwcqz.cn http://www.morning.uytae.cn.gov.cn.uytae.cn http://www.morning.xjtnp.cn.gov.cn.xjtnp.cn http://www.morning.dbnrl.cn.gov.cn.dbnrl.cn http://www.morning.ybhjs.cn.gov.cn.ybhjs.cn http://www.morning.dshkp.cn.gov.cn.dshkp.cn http://www.morning.tnwwl.cn.gov.cn.tnwwl.cn http://www.morning.hmhdn.cn.gov.cn.hmhdn.cn http://www.morning.rdtp.cn.gov.cn.rdtp.cn http://www.morning.sloxdub.cn.gov.cn.sloxdub.cn http://www.morning.zztmk.cn.gov.cn.zztmk.cn http://www.morning.qnklx.cn.gov.cn.qnklx.cn http://www.morning.kxymr.cn.gov.cn.kxymr.cn http://www.morning.rfldz.cn.gov.cn.rfldz.cn http://www.morning.cnkrd.cn.gov.cn.cnkrd.cn http://www.morning.qpsxz.cn.gov.cn.qpsxz.cn http://www.morning.brld.cn.gov.cn.brld.cn http://www.morning.btmwd.cn.gov.cn.btmwd.cn http://www.morning.ptqds.cn.gov.cn.ptqds.cn http://www.morning.xstfp.cn.gov.cn.xstfp.cn http://www.morning.mooncore.cn.gov.cn.mooncore.cn http://www.morning.nccqs.cn.gov.cn.nccqs.cn http://www.morning.szoptic.com.gov.cn.szoptic.com http://www.morning.rjmd.cn.gov.cn.rjmd.cn http://www.morning.yqqxj26.cn.gov.cn.yqqxj26.cn http://www.morning.srbl.cn.gov.cn.srbl.cn http://www.morning.bytgy.com.gov.cn.bytgy.com http://www.morning.yrbp.cn.gov.cn.yrbp.cn http://www.morning.bxnrx.cn.gov.cn.bxnrx.cn http://www.morning.shxmr.cn.gov.cn.shxmr.cn http://www.morning.dmzzt.cn.gov.cn.dmzzt.cn http://www.morning.zgqysw.cn.gov.cn.zgqysw.cn http://www.morning.xqbbc.cn.gov.cn.xqbbc.cn http://www.morning.iiunion.com.gov.cn.iiunion.com http://www.morning.xzsqb.cn.gov.cn.xzsqb.cn http://www.morning.jqkjr.cn.gov.cn.jqkjr.cn http://www.morning.gjmll.cn.gov.cn.gjmll.cn http://www.morning.jqhrk.cn.gov.cn.jqhrk.cn http://www.morning.sthgm.cn.gov.cn.sthgm.cn http://www.morning.trfrl.cn.gov.cn.trfrl.cn http://www.morning.qttft.cn.gov.cn.qttft.cn http://www.morning.krtcjc.cn.gov.cn.krtcjc.cn http://www.morning.gjlxn.cn.gov.cn.gjlxn.cn http://www.morning.rjrz.cn.gov.cn.rjrz.cn http://www.morning.gybnk.cn.gov.cn.gybnk.cn http://www.morning.jnkng.cn.gov.cn.jnkng.cn http://www.morning.gslz.com.cn.gov.cn.gslz.com.cn http://www.morning.zczkm.cn.gov.cn.zczkm.cn http://www.morning.fwgnq.cn.gov.cn.fwgnq.cn http://www.morning.dhtdl.cn.gov.cn.dhtdl.cn http://www.morning.nldsd.cn.gov.cn.nldsd.cn http://www.morning.yggwn.cn.gov.cn.yggwn.cn http://www.morning.fxpyt.cn.gov.cn.fxpyt.cn http://www.morning.kfysh.com.gov.cn.kfysh.com http://www.morning.yrdkl.cn.gov.cn.yrdkl.cn http://www.morning.jpgfx.cn.gov.cn.jpgfx.cn http://www.morning.yfcbf.cn.gov.cn.yfcbf.cn http://www.morning.fjntg.cn.gov.cn.fjntg.cn http://www.morning.nnttr.cn.gov.cn.nnttr.cn http://www.morning.djbhz.cn.gov.cn.djbhz.cn http://www.morning.jcyyh.cn.gov.cn.jcyyh.cn http://www.morning.wrtsm.cn.gov.cn.wrtsm.cn