• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell
您的位置:首页 > 脚本语言 >Golang > 深入理解Go语言中的数组和切片

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

作者:daisy 字体:[增加 减小] 来源:互联网

daisy 通过本文主要向大家介绍了go语言数组,go语言遍历数组,go 数组,go 数组初始化,go 创建数组等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

一、类型

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

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

我们来看一段代码

//a是一个数组,注意数组是一个固定长度的,初始化时候必须要指定长度,不指定长度的话就是切片了
a := [3]int{1, 2, 3}
//b是数组,是a的一份拷贝
b := a
//c是切片,是引用类型,底层数组是a
c := 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]
</div>

二、make

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

s1 := make([]int, 0, 3)

for i := 0; i < cap(s1); i++ {
 s1 = append(s1, i)
}
s2 := s1
for i := 0; i < len(a); i++ {
 s1[i] = s1[i] + 1
}

fmt.Println(s1) //[1 2 3]
fmt.Println(s2) //[1 2 3]
</div>

三、当对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
//0xc20801e170
fmt.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的j
n2 = 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
//0xc20801e170

fmt.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
</div>

四、引用“失效”

实现了删除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)
}
</div>

调用此函数后,发现原来的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]
}
</div>

打印出来的结果如下:

[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]
</div>

这里直接打印了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]
}
</div>

结果:

[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]
</div>

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

总结

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

</div>

您可能想查找下面的文章:

  • 深入理解Go语言中的数组和切片
  • Go语言中数组的基本用法演示
  • Go语言获取数组长度的方法
  • Go语言算法之寻找数组第二大元素的方法
  • Go语言创建、初始化数组的常见方式汇总
  • GO语言基础之数组
  • GO语言数组和切片实例详解

相关文章

  • Go语言中的复合类型详细介绍
  • Go语言中的range用法实例分析
  • go语言异常panic和恢复recover用法实例
  • Golang学习笔记(二):类型、变量、常量
  • golang实现http服务器处理静态文件示例
  • Go语言实现的最简单数独解法
  • Go语言map字典用法实例分析
  • 在Mac中搭建go语言开发环境的操作步骤
  • GO语言make()分配用法实例
  • GO语言利用K近邻算法实现小说鉴黄

文章分类

  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell

最近更新的内容

    • golang网络socket粘包问题的解决方法
    • Golang中的sync.WaitGroup用法实例
    • Go语言的方法接受者类型用值类型还是指针类型?
    • Golang极简入门教程(一):基本概念
    • Go语言入门教程之Arrays、Slices、Maps、Range操作简明总结
    • go语言实现简单http服务的方法
    • 从Node.js 转到 Go平台
    • Go语言中的内存布局详解
    • 学习GO编程必备知识汇总
    • Go语言多值替换的HTML模板实例分析

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有