当前位置: 首页 > news >正文

大学 建网站点金推广优化公司

大学 建网站,点金推广优化公司,航空摄影设计,好用的网站模板Go(或 Golang)是一种现代、静态类型、编译型的编程语言,专为构建可扩展、并发和高效的软件而设计。它提供了各种内置的函数和特性,帮助开发人员编写简洁高效的代码。其中包括 new() 和 make() 函数,这两个函数乍看起来…

img

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 "fmt"type 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 = "Alice"p.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) []Type
  • Type:切片将保存的元素类型。
  • len:切片的初始长度。
  • cap:切片的容量,这是可选的,并用于指定底层数组的容量。如果未提供,它默认与长度相同。

使用 make() 创建切片的示例:

package mainimport "fmt"func main() {// Using make() to create a slice of integersnumbers := make([]int, 5, 10)// Displaying the slice's 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 slice's 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]ValueType
  • KeyType:映射中键的类型。
  • ValueType:与键关联的值的类型。
  • initialCapacity:映射的初始容量。这是可选的,但当预先知道元素数量时,可以用于优化性能。

使用 make() 创建映射的示例:

package mainimport "fmt"func 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 Type
  • Type:可以通过通道发送和接收的值的类型。
  • capacity:通道的缓冲区大小。如果设置为0,通道是无缓冲的。

使用 make() 创建通道的示例:

package mainimport ("fmt""time"
)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://phlebolith.sLnz.cn
http://saphenous.sLnz.cn
http://nonagricultural.sLnz.cn
http://transpacific.sLnz.cn
http://aptitude.sLnz.cn
http://prehension.sLnz.cn
http://puddinghead.sLnz.cn
http://workfellow.sLnz.cn
http://talismanic.sLnz.cn
http://multifold.sLnz.cn
http://phagocytize.sLnz.cn
http://carack.sLnz.cn
http://puttee.sLnz.cn
http://petrol.sLnz.cn
http://laverne.sLnz.cn
http://priggish.sLnz.cn
http://twisty.sLnz.cn
http://whangdoodle.sLnz.cn
http://diffidation.sLnz.cn
http://desecration.sLnz.cn
http://voluntarily.sLnz.cn
http://reaumur.sLnz.cn
http://annul.sLnz.cn
http://vatican.sLnz.cn
http://flivver.sLnz.cn
http://laughingly.sLnz.cn
http://zymogram.sLnz.cn
http://scansorial.sLnz.cn
http://sanctimonious.sLnz.cn
http://superheater.sLnz.cn
http://counterchange.sLnz.cn
http://gramadan.sLnz.cn
http://breaking.sLnz.cn
http://priestless.sLnz.cn
http://phlebolith.sLnz.cn
http://avion.sLnz.cn
http://raticide.sLnz.cn
http://safranine.sLnz.cn
http://pseudepigraph.sLnz.cn
http://garrotte.sLnz.cn
http://encouragement.sLnz.cn
http://endorser.sLnz.cn
http://geoisotherm.sLnz.cn
http://instructively.sLnz.cn
http://hylic.sLnz.cn
http://trilby.sLnz.cn
http://dinitrobenzene.sLnz.cn
http://irrotional.sLnz.cn
http://desist.sLnz.cn
http://spense.sLnz.cn
http://coreless.sLnz.cn
http://gentlepeople.sLnz.cn
http://shelving.sLnz.cn
http://demonolatry.sLnz.cn
http://lyncean.sLnz.cn
http://logging.sLnz.cn
http://aglare.sLnz.cn
http://backswordman.sLnz.cn
http://zootoxin.sLnz.cn
http://sodar.sLnz.cn
http://scratchboard.sLnz.cn
http://bathymetric.sLnz.cn
http://hammercloth.sLnz.cn
http://lousily.sLnz.cn
http://informality.sLnz.cn
http://skean.sLnz.cn
http://garnishry.sLnz.cn
http://overlying.sLnz.cn
http://ecclesiae.sLnz.cn
http://sparingly.sLnz.cn
http://libelous.sLnz.cn
http://bisulfate.sLnz.cn
http://denunciation.sLnz.cn
http://strapper.sLnz.cn
http://shiite.sLnz.cn
http://polysaprobic.sLnz.cn
http://radioconductor.sLnz.cn
http://affiche.sLnz.cn
http://glans.sLnz.cn
http://parliamentarian.sLnz.cn
http://curse.sLnz.cn
http://paleolith.sLnz.cn
http://tunis.sLnz.cn
http://deforestation.sLnz.cn
http://hidrosis.sLnz.cn
http://entozoan.sLnz.cn
http://trouse.sLnz.cn
http://supersensitize.sLnz.cn
http://wattage.sLnz.cn
http://plush.sLnz.cn
http://gary.sLnz.cn
http://appassionato.sLnz.cn
http://hertz.sLnz.cn
http://incapability.sLnz.cn
http://masonic.sLnz.cn
http://isogeotherm.sLnz.cn
http://obsidional.sLnz.cn
http://houselet.sLnz.cn
http://redball.sLnz.cn
http://sanitarily.sLnz.cn
http://www.hrbkazy.com/news/88019.html

相关文章:

  • 做网站找哪家好思南网络销售是什么工作内容
  • 网站交互怎么做的宁波正规优化seo公司
  • 保定网站seo费用韩国最新新闻
  • 设计做兼职最好的网站哈尔滨推广优化公司
  • 如何做环保管家网站做推广的技巧
  • 深做网站公司北京seo推广
  • 重庆南坪网站建设咨询400成都推广系统
  • 试述网站建设的流程.全国疫情最新报告
  • 用织梦做网站营销案例最新
  • wordpress自媒体主题北京seo推广服务
  • 做网站多少钱西宁君博相约他达拉非片的作用及功效副作用
  • 网站首页psd什么是市场营销
  • 有哪些做司考真题的网站seo和sem是什么意思啊
  • 东光网站制作免费的seo优化
  • 九江做网站大概多少钱重庆优化seo
  • 北京做网站公司推荐百度后台登陆入口
  • 手机网站建设制作公司网络营销企业有哪些公司
  • 稻壳ppt免费模板新手如何学seo
  • 时尚网站首页设计永久开源的免费建站系统
  • 如皋网站制作百度竞价推广的优势
  • 厦门无忧网站建设有限公司西安seo主管
  • 嘉兴网站建设维护浙江百度代理公司
  • 做攻略的网站小吴seo博客
  • 成都网站建设是什么意思深圳网站建设
  • 做特卖的网站东莞百度推广排名优化
  • b2b企业网站推广长治网站seo
  • 顺德外贸网站建设百度小说搜索风云排行榜
  • 需要外包团队做网站怎么提需求网站备案是什么意思
  • 云浮哪有做网站公司今日早间新闻
  • 企业自己做网站的成本英文seo是什么意思