侯体宗的博客
  • 首页
  • Hyperf版
  • beego仿版
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 中国象棋ai
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

深入理解Go语言中的数组和切片

Go  /  管理员 发布于 5年前   216

一、类型

数组是值类型,将一个数组赋值给另一个数组时,传递的是一份拷贝。

切片是引用类型,切片包装的数组称为该切片的底层数组。

我们来看一段代码

//a是一个数组,注意数组是一个固定长度的,初始化时候必须要指定长度,不指定长度的话就是切片了a := [3]int{1, 2, 3}//b是数组,是a的一份拷贝b := a//c是切片,是引用类型,底层数组是ac := a[:]for i := 0; i < len(a); i++ { a[i] = a[i] + 1}//改变a的值后,b是a的拷贝,b不变,c是引用,c的值改变fmt.Println(a) //[2,3,4]fmt.Println(b) //[1 2 3]fmt.Println(c) //[2,3,4]

二、make

make 只能用于slice, map 和 channel, 所以下面一段代码生成了一个slice,是引用类型

s1 := make([]int, 0, 3)for i := 0; i < cap(s1); i++ { s1 = append(s1, i)}s2 := s1for i := 0; i < len(a); i++ { s1[i] = s1[i] + 1}fmt.Println(s1) //[1 2 3]fmt.Println(s2) //[1 2 3]

三、当对slice append 超出底层数组的界限时

//n1是n2的底层数组n1 := [3]int{1, 2, 3}n2 := n1[0:3]fmt.Println("address of items in n1: ")for i := 0; i < len(n1); i++ { fmt.Printf("%p\n", &n1[i])}//address of items in n1://0xc20801e160//0xc20801e168//0xc20801e170fmt.Println("address of items in n2: ")for i := 0; i < len(n2); i++ { fmt.Printf("%p\n", &n2[i])}//address of items in n2://0xc20801e160//0xc20801e168//0xc20801e170//对n2执行append操作后,n2超出了底层数组n1的jn2 = append(n2, 1)fmt.Println("address of items in n1: ")for i := 0; i < len(n1); i++ { fmt.Printf("%p\n", &n1[i])}//address of items in n1://0xc20801e160//0xc20801e168//0xc20801e170fmt.Println("address of items in n2: ")for i := 0; i < len(n2); i++ { fmt.Printf("%p\n", &n2[i])}//address of items in n2://0xc20803a2d0//0xc20803a2d8//0xc20803a2e0//0xc20803a2e8

四、引用“失效”

实现了删除slice最后一个item的函数

func rmLast(a []int) { fmt.Printf("[rmlast] the address of a is %p", a) a = a[:len(a)-1] fmt.Printf("[rmlast] after remove, the address of a is %p", a)}

调用此函数后,发现原来的slice并没有改变

func main() { xyz := []int{1, 2, 3, 4, 5, 6, 7, 8, 9} fmt.Printf("[main] the address of xyz is %p\n", xyz) rmLast(xyz) fmt.Printf("[main] after remove, the address of xyz is %p\n", xyz) fmt.Printf("%v", xyz) //[1 2 3 4 5 6 7 8 9]}

打印出来的结果如下:

[main] the address of xyz is 0xc2080365f0[rmlast] the address of a is 0xc2080365f0[rmlast] after remove, the address of a is 0xc2080365f0[main] after remove, the address of xyz is 0xc2080365f0[1 2 3 4 5 6 7 8 9]

这里直接打印了slice的指针值,因为slice是引用类型,所以指针值都是相同的,我们换成打印slice的地址看下

func rmLast(a []int) { fmt.Printf("[rmlast] the address of a is %p", &a) a = a[:len(a)-1] fmt.Printf("[rmlast] after remove, the address of a is %p", &a)}func main() { xyz := []int{1, 2, 3, 4, 5, 6, 7, 8, 9} fmt.Printf("[main] the address of xyz is %p\n", &xyz) rmLast(xyz) fmt.Printf("[main] after remove, the address of xyz is %p\n", &xyz) fmt.Printf("%v", xyz) //[1 2 3 4 5 6 7 8 9]}

结果:

[main] the address of xyz is 0xc20801e1e0[rmlast] the address of a is 0xc20801e200[rmlast] after remove, the address of a is 0xc20801e200[main] after remove, the address of xyz is 0xc20801e1e0[1 2 3 4 5 6 7 8 9]

这次可以看到slice作为函数参数传入函数时,实际上也是拷贝了一份slice,因为slice本身是个指针,所以从现象来看,slice是引用类型

总结

以上就是这篇文章的全部内容,希望对大家的学习或者工作带来一定的帮助,如果有疑问大家可以留言交流。


  • 上一条:
    总结Go语言中defer的使用和注意要点
    下一条:
    Go语言实现类似c++中的多态功能实例
  • 昵称:

    邮箱:

    0条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • 在go中实现一个常用的先进先出的缓存淘汰算法示例代码(0个评论)
    • 在go+gin中使用"github.com/skip2/go-qrcode"实现url转二维码功能(0个评论)
    • 在go语言中使用api.geonames.org接口实现根据国际邮政编码获取地址信息功能(1个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf分页文件功能(0个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf文件功能(0个评论)
    • 近期文章
    • 智能合约Solidity学习CryptoZombie第三课:组建僵尸军队(高级Solidity理论)(0个评论)
    • 智能合约Solidity学习CryptoZombie第二课:让你的僵尸猎食(0个评论)
    • 智能合约Solidity学习CryptoZombie第一课:生成一只你的僵尸(0个评论)
    • 在go中实现一个常用的先进先出的缓存淘汰算法示例代码(0个评论)
    • 在go+gin中使用"github.com/skip2/go-qrcode"实现url转二维码功能(0个评论)
    • 在go语言中使用api.geonames.org接口实现根据国际邮政编码获取地址信息功能(1个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf分页文件功能(0个评论)
    • gmail发邮件报错:534 5.7.9 Application-specific password required...解决方案(0个评论)
    • 欧盟关于强迫劳动的规定的官方举报渠道及官方举报网站(0个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf文件功能(0个评论)
    • 近期评论
    • 122 在

      学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..
    • 123 在

      Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..
    • 原梓番博客 在

      在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..
    • 博主 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..
    • 1111 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
    • 2016-10
    • 2017-09
    • 2020-03
    • 2020-05
    • 2020-06
    • 2020-07
    • 2020-12
    • 2021-01
    • 2021-05
    • 2021-06
    • 2021-07
    • 2021-08
    • 2021-10
    • 2021-11
    • 2021-12
    • 2022-01
    • 2022-02
    • 2022-03
    • 2022-04
    • 2022-05
    • 2022-06
    • 2022-07
    • 2022-08
    • 2022-09
    • 2022-10
    • 2022-11
    • 2022-12
    • 2023-01
    • 2023-02
    • 2023-03
    • 2023-04
    • 2023-05
    • 2023-06
    • 2023-07
    • 2023-08
    • 2023-09
    • 2023-10
    • 2023-11
    • 2023-12
    • 2024-01
    • 2024-02
    • 2024-03
    • 2024-04
    • 2024-05
    • 2024-06
    • 2024-07
    • 2024-08
    • 2024-11
    • 2025-02
    • 2025-04
    • 2025-05
    • 2025-06
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客