美文网首页Go知识库互联网就业技术指南Go常用库
GO语言面试系列:(一)深入理解Go语言中的数组和切片

GO语言面试系列:(一)深入理解Go语言中的数组和切片

作者: Chole121 | 来源:发表于2018-10-18 10:56 被阅读59次

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

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

我们来看一段代码

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

二、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] 

三、当对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 

四、引用“失效”
实现了删除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是引用类型

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

添加客服微信:grey0805,加入知识学习小分队~!

相关文章

  • GO语言面试系列:(一)深入理解Go语言中的数组和切片

    一、类型数组是值类型,将一个数组赋值给另一个数组时,传递的是一份拷贝。 切片是引用类型,切片包装的数组称为该切片的...

  • Go语言第3天 - 常用数据类型

    以下内容部分参考自Go语言基础数据类型Go语言中的数组切片:特立独行的可变数组Go语言数据类型-数组Go标准容器之...

  • 八、Go range的用法

    八、Go语言range Go 语言中 range 关键字用于 for 循环中迭代数组(array)、切片(slic...

  • Go语言切片

    // //Go语言切片 // /* // go语言切片是对数组的抽象 // Go 数组的长度不可改变,在特定场景中...

  • 3、Go语言的数组.md

    前面 Go 语言的变量和类型那一节提到了数组,数组在 Go 语言中是非常基础的一个东西,虽然后面的切片比数组更常用...

  • 11-Go语言数组和切片-指趣学院

    数组 和C语言一样,Go语言中也有数组的概念, Go语言中的数组也是用于保存一组相同类型的数据 和C语言一样,Go...

  • Go 语言程序设计——集合类型(1)

    Go语言的所有内置类型:数组、切片和映射 值、指针和引用类型 值在传递给函数或者方法的时候会被复制一次 Go语言中...

  • 七、Go切片

    七、Go语言切片(Slice) Go 语言切片是对数组的抽象。 Go 数组的长度不可改变,在特定场景中这样的集合就...

  • Go每日精选(2019-06-14)- 基础部分

    1. Go语言的传参和传引用 2. PHP转Go系列:数组与切片 3. Golang 入门 : channel(...

  • Go-数组与slice

    本文将讲解Go语言中的数组与slice。之前看到网上好多 《深入理解slice》、《深入解析slice》... 的...

网友评论

    本文标题:GO语言面试系列:(一)深入理解Go语言中的数组和切片

    本文链接:https://www.haomeiwen.com/subject/lssjzftx.html