深入理解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
投稿
猜你喜欢
- Python中将列表转换成为数据框有两种情况:第一种是两个不同列表转换成一个数据框,第二种是一个包含不同子列表的列表转换成为数据框。第一种:
- 发送端代码:#!/usr/bin/python# -*- coding: UTF-8 -*-import socketimport stru
- 当然,第一反应是用存储过程。判断原来这个字段值,然后UPDATE。 网上粗粗找了一下没找到方案。自己一动手,居然有个很有趣的结果,连WHER
- 上传html文件内容如下:操作步骤<html><head><meta http-equiv="con
- python中的paramiko模块是用来实现ssh连接到远程服务器上的库,在进行连接的时候,可以用来执行命令,也可以用来上传文件。1、得到
- pandas读取txt文件读取txt文件需要确定txt文件是否符合基本的格式,也就是是否存在\t,,,等特殊的分隔符一般txt文件长成这个样
- 今天整理之前写的代码,发现在做数模期间写的用python实现的遗传算法,感觉还是挺有意思的,就拿出来分享一下。首先遗传算法是一种优化算法,通
- 目录Uiautomator2的安装和使用具体实现如何打开支付宝并进入蚂蚁森林?收能量如何停止完整代码结语@[toc] 虽然我支付宝加了好多好
- 1. 前言python除了丰富的第三方库外,本身也提供了一些内在的方法和底层的一些属性,大家比较常用的如dict、list、set、min、
- argparse模块用法一、 概念argsparse是python的命令行解析的标准模块,内置于python,不需要安装。这个库可以让我们直
- 前言要将图片转换为字符图其实很简单,我们首先将图片转换为灰度图像,这样图片的每个像素点的颜色值都是0到255,然后我们选用一些在文字矩形框内
- 本文实例讲述了SQL Server简单实现数据的日报和月报功能。分享给大家供大家参考,具体如下:--320, SQL SERVER 日报--
- 前言我们在上一期学习了关于Python 迭代器Iterator详情相关的概念,满足迭代器需要符合两个条件实现__iter__()方
- 本文实例讲述了python实现查找两个字符串中相同字符并输出的方法。分享给大家供大家参考。具体实现方法如下:seq1 = "spa
- 技术背景对于各行各业的研究人员来说,经常会面临这样的一个问题:有一篇不错的文章里面有很好的数据,但是这个数据在文章中仅以图片的形式出现。而假
- 1,创建测试表CREATE TABLE `testsign` ( `userid` int(5) DEFAULT NULL, `user
- 转换为字符串类型tips['sex_str'] = tips['sex'].astype(str)转换为数值
- --创建数据库(文件:主要数据文件mdf==1,次要数据文件ndf>=0,日志文件ldf>=1)--文件组:当1mdf,5个nd
- 本文实例讲述了Python3.5迭代器与生成器用法。分享给大家供大家参考,具体如下:1、列表生成式通过列表生成式可以直接创建一个列表。代码:
- 一、备份数据库 1、打开SQL企业管理器,在控制台根目录中依次点开Microsoft SQL Server