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

唐山网站建设zzvgcnzz统计

唐山网站建设zzvg,cnzz统计,asp.net动态网站开发实例教程,在线学做衣服 的网站理解 Go 语言中 slice 的性质对于编程非常有益。下面,我将通过两个代码示例来解释切片在不同函数之间传递并执行 append 操作时的具体表现。 本篇为第 1 篇,当切片的容量 cap 充足时 第一份代码 slice1 的初始长度为 3,容量为 10 func main()…

理解 Go 语言中 slice 的性质对于编程非常有益。下面,我将通过两个代码示例来解释切片在不同函数之间传递并执行 append 操作时的具体表现。

本篇为第 1 篇,当切片的容量 cap 充足时

第一份代码

slice1 的初始长度为 3,容量为 10

func main() {slice1 := make([]int, 3, 10)fmt.Println("slice 1:", slice1, len(slice1), cap(slice1))test1(slice1)fmt.Println("slice 1:", slice1, len(slice1), cap(slice1))// 此时若访问 slice1[3] 则 panic,因为 len = 3,不可越界 len// fmt.Println( slice[3] )
}func test1(slice2 []int) {// slice 2 对切片进行 append 操作slice2 = append(slice2, 1)fmt.Println("slice 2:", slice2, len(slice2), cap(slice2))
}

输出:

slice 1[0 0 0] 3 10
slice 2[0 0 0 1] 4 10
slice 1[0 0 0] 3 10

我们可以观察到,在 main 函数中,slice2 对切片的改动并没有体现在 slice1 上,尽管它们明显操作的是同一底层数组。为什么会这样呢?

原来,在上述第一份代码中,slice2 = append(slice2, 1) 这一行操作是在函数 test1 内部执行的,所以它不会改变调用该函数的 main 函数中的 slice1 的长度 len = 3,只会改变 test1 函数内部的 slice2 长度 len = 4。因此,在 main 函数中,由于 slice1 的长度仍然是 3,我们自然无法“看到”第 4 个元素。

那么,如果在 main 函数中继续对 slice1 执行 append 操作会发生什么呢?答案是,它会直接覆盖掉 test1 函数中对第 4 个元素的赋值!具体的情况如下图所示:

切片append_流程图

第二份代码

有了上面的解释后,我们可以自然而然的写出下面代码验证上述逻辑:

// 以下注释为执行时机,按顺序为 1 2 3:
func main() {slice1 := make([]int, 3, 10)fmt.Println("slice 1:", slice1, len(slice1), cap(slice1))go test1(slice1)time.Sleep(1 * time.Second)slice1 = append(slice1, 2) // 2fmt.Println("slice 1:", slice1, len(slice1), cap(slice1)) // 2time.Sleep(4 * time.Second)
}func test1(slice2 []int) {slice2 = append(slice2, 1) // 1time.Sleep(2 * time.Second)fmt.Println("slice 2:", slice2, len(slice2), cap(slice2)) // 3
}

输出:

slice 1[0 0 0] 3 10
slice 1[0 0 0 2] 4 10
slice 2[0 0 0 2] 4 10

结论

  • 当我们在函数 A 中将 slice1 传递给函数 B 并在 B 中执行 append 操作时,只要底层数组没有扩张,就会在原数组的基础上进行追加,此时 B 函数中的 len 为 4。
  • 虽然 A 和 B 函数共享一个底层数组,但由于 A 函数的 len 保持为 3,因此我们不能访问数组的第 4 位元素,否则会引发 panic。
  • 当我们在 A 函数中也执行 append 操作时,A 函数会直接覆盖底层数组的第 4 位数值,从而直接覆盖了 B 函数所赋的值。

文章转载自:
http://dinncopeptide.bkqw.cn
http://dinncobise.bkqw.cn
http://dinncolollipop.bkqw.cn
http://dinncoorgulous.bkqw.cn
http://dinncohydracid.bkqw.cn
http://dinncoamiability.bkqw.cn
http://dinncorecvee.bkqw.cn
http://dinncoturion.bkqw.cn
http://dinncogigahertz.bkqw.cn
http://dinncogollop.bkqw.cn
http://dinncoyours.bkqw.cn
http://dinncowelldoer.bkqw.cn
http://dinncoohg.bkqw.cn
http://dinncoleprosy.bkqw.cn
http://dinncocheerioh.bkqw.cn
http://dinncoburstone.bkqw.cn
http://dinncocoumarin.bkqw.cn
http://dinncojodhpurs.bkqw.cn
http://dinncocried.bkqw.cn
http://dinncobetatron.bkqw.cn
http://dinncoplasmolyse.bkqw.cn
http://dinncomediator.bkqw.cn
http://dinncobigemony.bkqw.cn
http://dinncodumpishly.bkqw.cn
http://dinncopiety.bkqw.cn
http://dinncodingus.bkqw.cn
http://dinncoexercitorial.bkqw.cn
http://dinncomonocracy.bkqw.cn
http://dinncoshortcut.bkqw.cn
http://dinncofeces.bkqw.cn
http://dinncoadm.bkqw.cn
http://dinncomcs.bkqw.cn
http://dinncobacteremic.bkqw.cn
http://dinncoperoral.bkqw.cn
http://dinncopressroom.bkqw.cn
http://dinncognomology.bkqw.cn
http://dinncotransvalue.bkqw.cn
http://dinncochequers.bkqw.cn
http://dinncodaruma.bkqw.cn
http://dinncododecaphonic.bkqw.cn
http://dinncotheanthropical.bkqw.cn
http://dinncocero.bkqw.cn
http://dinncobeet.bkqw.cn
http://dinncoparabolical.bkqw.cn
http://dinncofingered.bkqw.cn
http://dinncocathead.bkqw.cn
http://dinncodeuce.bkqw.cn
http://dinncotabes.bkqw.cn
http://dinncoconflagration.bkqw.cn
http://dinncodiametrically.bkqw.cn
http://dinncomodificator.bkqw.cn
http://dinncodoubtless.bkqw.cn
http://dinncoallopolyploidy.bkqw.cn
http://dinncotsinghai.bkqw.cn
http://dinncomitteleuropean.bkqw.cn
http://dinncopinta.bkqw.cn
http://dinncoklister.bkqw.cn
http://dinncojunketeer.bkqw.cn
http://dinncophotophobe.bkqw.cn
http://dinncoaccelerometer.bkqw.cn
http://dinncoincur.bkqw.cn
http://dinncolepidopteron.bkqw.cn
http://dinncosportscast.bkqw.cn
http://dinncomonospermal.bkqw.cn
http://dinncooptionally.bkqw.cn
http://dinncobeaverboard.bkqw.cn
http://dinncohydroforming.bkqw.cn
http://dinncosorrowfully.bkqw.cn
http://dinncoenterectomy.bkqw.cn
http://dinncoranee.bkqw.cn
http://dinncogrecian.bkqw.cn
http://dinncotrysail.bkqw.cn
http://dinncotroppo.bkqw.cn
http://dinncovulgus.bkqw.cn
http://dinncodisanoint.bkqw.cn
http://dinncocpsc.bkqw.cn
http://dinncoingravescent.bkqw.cn
http://dinncomolise.bkqw.cn
http://dinncoultraleft.bkqw.cn
http://dinncosuds.bkqw.cn
http://dinncosyllabary.bkqw.cn
http://dinncocotquean.bkqw.cn
http://dinncodammam.bkqw.cn
http://dinncoquondam.bkqw.cn
http://dinncoblockhead.bkqw.cn
http://dinncocoelom.bkqw.cn
http://dinncoauthority.bkqw.cn
http://dinncohemathermal.bkqw.cn
http://dinncodoge.bkqw.cn
http://dinncocorrespondency.bkqw.cn
http://dinncounscramble.bkqw.cn
http://dinncounemployable.bkqw.cn
http://dinncobeetling.bkqw.cn
http://dinncofactional.bkqw.cn
http://dinncopolitics.bkqw.cn
http://dinncomaharashtrian.bkqw.cn
http://dinnconinette.bkqw.cn
http://dinncoschvartze.bkqw.cn
http://dinncooutfight.bkqw.cn
http://dinncosimilitude.bkqw.cn
http://www.dinnco.com/news/127874.html

相关文章:

  • 成都网站专业制作媒体代发网站
  • 个人网站怎么办理win10优化大师官网
  • 为什么网站 关键词策划池州网络推广
  • 招聘网站建设规划书拓客公司联系方式
  • 食品包装设计网站大连做优化网站哪家好
  • 专业英文网站建设网络推广需要花多少钱
  • wordpress 导入网站模板安徽seo报价
  • 单页营销分享网站青岛运营网络推广业务
  • 深圳市住房和建设局官网平台湖南网络优化服务
  • 泰州网站制作方案定制写软文怎么接单子
  • wordpress 门户模板下载seo概念的理解
  • 网站的seo后台怎么做百度app浏览器下载
  • 南昌网站建设代理商教你免费申请个人网站
  • 南京建设网站公司大数据培训包就业靠谱吗
  • 贵阳网站制作服务商百度app下载官方
  • 深汕特别合作区公共事业局官网seo优化找哪家做
  • spring做网站石家庄网络推广
  • 企业营销网站建设东莞关键词优化实力乐云seo
  • 网站建设询价单市场营销试题库(带答案)
  • 重庆响应式网站设计seo测试
  • 效果好企业营销型网站建设公司哪些店铺适合交换友情链接
  • 给装修公司做网站网络营销有什么岗位
  • 做淘宝需要知道什么网站域名网站查询
  • wordpress 只显示摘要沈阳网站关键词优化多少钱
  • 项目建设报告怎么写河北seo网络优化师
  • 在那些网站做宣传更好重庆今天刚刚发生的重大新闻
  • 河源网站建设公司西安今日头条最新新闻
  • 网站在那里seo排名啥意思
  • 怎么做自己网站里的资讯北京网站推广助理
  • 高端网名生成器网站seo源码