深入理解Go语言中的数组和切片
作者:daisy 发布时间:2024-02-04 05:09:54
标签:go语言,数组,切片
一、类型
数组是值类型,将一个数组赋值给另一个数组时,传递的是一份拷贝。
切片是引用类型,切片包装的数组称为该切片的底层数组。
我们来看一段代码
//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
是引用类型
总结
0
投稿
猜你喜欢
- 代码如下: 代码如下:///<summary> /// 将两个列不同的DataTable合并成一个新的DataTab
- 无意中看到以前在电脑上保存的一个html页面,关于div水平垂直居中的问题。如何实现div水平垂直居中呢?1.已知宽高度水平垂直居中posi
- 总说由于pytorch 0.4版本更新实在太大了, 以前版本的代码必须有一定程度的更新. 主要的更新在于 Variable和Tensor的合
- 这两天一直在看tensorflow中的读取数据的队列,说实话,真的是很难懂。也可能我之前没这方面的经验吧,最早我都使用的theano,什么都
- ExecuteReader(),ExecuteNonQuery(),ExecuteScalar(),ExecuteXmlReader()之间
- 本文实例讲述了Python wxPython库Core组件BoxSizer用法。分享给大家供大家参考,具体如下:wx.BoxSizer:bo
- 本文实例讲述了Python使用装饰器模拟用户登陆验证功能。分享给大家供大家参考,具体如下:# -*- coding:utf-8 -*-#!p
- 本文实例讲述了Symfony2之session与cookie用法。分享给大家供大家参考,具体如下:session操作:1. Set Sess
- 背景:用python画AR模型的时序图。结果:代码:import numpy as npimport matplotlib.pyplot a
- 本文是OpenCV图像视觉入门之路的第11篇文章,本文详细的在图像形态学进行了图像处理,例如:腐蚀操作、膨胀操作、开闭运算、梯度运算、Top
- 由于工作中涉及到生日编辑资料编辑,然后自己改了一下代码:<html><head> <meta charset=
- a {}{ background:url(images/normal.gif); } a:ho
- 记得刚学python那会,作过一个飞机大战小项目,这个项目非常经典,可以帮助初学者提高动手能力,今天把它分享出来。一、项目介绍先放几张图片二
- 因为他的简单实用和数量庞大的插件,所以我们喜欢和使用jQuery,在jQuery数量庞大的插件中有很大的一部分是关于图片的,所以今天我们就整
- 1. txt文件(1) 单位矩阵即主对角线上的元素均为1,其余元素均为0的正方形矩阵。在NumPy中可以用eye函数创建一个这样的二维数组,
- 上周 RealWorld CTF 2018 web 题 bookhub 有个未授权访问的漏洞,比较有意思,赛后看了一下公开的 WriteUp
- 可实现类似于sql中的dateadd、datesub的功能两种获取日期的方式z=datetime.datetime(2016,12,5)z=
- 概述在使用Typora写比较长的文章时,需要给章节编号,方便区分层次。如果手动编号,一旦章节顺序改变,很多章节的编号都需要一一手动修改,极其
- 1998年,W3C发布HTML 4.0 Specification,里面清清楚楚的写了每个标签的用法和语义。搜索引擎的算法参考了W3C的语义
- 本文实例讲述了Symfony2实现从数据库获取数据的方法。分享给大家供大家参考,具体如下:假设有一张表:test, 字段:name,colo