golang 数组和切片

本文详细介绍了Go语言中数组和切片在内存分配上的区别,包括深浅拷贝,以及Go1.18及后续版本的扩容规则变化,如容量增长阈值和比例调整。

区别
1.数组长度固定,切片长度可变
2.数组是深拷贝,切片是浅拷贝,切片是引用类型

扩容规则 不同版本不一样
https://www.jb51.net/article/280481.htm#_lab2_2_1

go1.18
1.如果期望容量大于当前容量的两倍就会使用期望容量;
2.如果当前切片的容量小于阈值(默认 256)就会将容量翻倍;
3.如果当前切片的容量大于等于阈值(默认 256),小切片的2倍生长,大切片生长1.25倍;

package main

import "fmt"

func main() {

	arr1 := [3]int{1, 2, 3}
	arr2 := arr1
	fmt.Printf("%p\n", &arr1[0])
	fmt.Printf("%p\n", &arr2[0])
	arr2[0] = 10
	fmt.Println(arr1)
	fmt.Println(arr2)

	sli1 := []int{1, 2}
	sli2 := sli1
	fmt.Printf("%p\n", &sli1[0])
	fmt.Printf("%p\n", &sli2[0])
	sli2[0] = 10
	fmt.Println(sli1)
	fmt.Println(sli2)

	sliceAppend(sli1)
	fmt.Println(sli1)
}

// 不会改变原有slice长度
func sliceAppend(s []int) {
	s = append(s, []int{2, 2, 2}...)
	fmt.Printf("sliceAppend %d %d\n", len(s), len(s))
}

package main

import "fmt"

func main() {
	sli1 := []int{1}
	fmt.Printf("len=%d cap=%d\n", len(sli1), cap(sli1))

	// 期望容量大于当前容量的两倍
	sli1 = append(sli1, []int{2, 3, 4}...)
	fmt.Printf("len=%d cap=%d\n", len(sli1), cap(sli1))

	// 如果当前切片的容量小于阈值(默认 256)就会将容量翻倍
	sli1 = append(sli1, 1)
	fmt.Printf("len=%d cap=%d\n", len(sli1), cap(sli1))

	// 如果当前切片的容量大于等于阈值(默认 256),就会每次增加 25% 的容量
	/*
		//从小切片的2倍生长过渡
		//大切片生长1.25倍
		newcap += (newcap + 3*threshold) / 4
	*/
	sli2 := make([]int, 256, 256)
	fmt.Printf("len=%d cap=%d\n", len(sli2), cap(sli2))
	sli2 = append(sli2, 1)
	fmt.Printf("len=%d cap=%d\n", len(sli2), cap(sli2))

	for i := 0; i < 10; i++ {
		sli2 = append(sli2, sli2...)
		fmt.Printf("len=%d cap=%d\n", len(sli2), cap(sli2))
	}

}

runtime/slice

type slice struct {
	array unsafe.Pointer
	len   int
	cap   int
}

func growslice(et *_type, old slice, cap int) slice {
	if raceenabled {
		callerpc := getcallerpc()
		racereadrangepc(old.array, uintptr(old.len*int(et.size)), callerpc, abi.FuncPCABIInternal(growslice))
	}
	if msanenabled {
		msanread(old.array, uintptr(old.len*int(et.size)))
	}
	if asanenabled {
		asanread(old.array, uintptr(old.len*int(et.size)))
	}

	if cap < old.cap {
		panic(errorString("growslice: cap out of range"))
	}

	if et.size == 0 {
		// append should not create a slice with nil pointer but non-zero len.
		// We assume that append doesn't need to preserve old.array in this case.
		return slice{unsafe.Pointer(&zerobase), old.len, cap}
	}

	newcap := old.cap
	doublecap := newcap + newcap
	if cap > doublecap {
		newcap = cap
	} else {
		const threshold = 256
		if old.cap < threshold {
			newcap = doublecap
		} else {
			// Check 0 < newcap to detect overflow
			// and prevent an infinite loop.
			for 0 < newcap && newcap < cap {
				// Transition from growing 2x for small slices
				// to growing 1.25x for large slices. This formula
				// gives a smooth-ish transition between the two.
				newcap += (newcap + 3*threshold) / 4
			}
			// Set newcap to the requested cap when
			// the newcap calculation overflowed.
			if newcap <= 0 {
				newcap = cap
			}
		}
	}

<think>首先,用户的问题是:“golang 数组切片”,翻译成英文是“Golang array take slice”。我需要用中文回答这个问题。问题似乎是关于如何在Go语言中从数组获取切片。 关键点是: - 使用中文回答问题。 - 回答后,必须提出3-5个相关问题。 - 相关问题必须以“§§相关问题§§”开头,且在第一行完整输出。 - 禁止换行分散,意思是相关问题的列表应该紧凑,没有额外的换行。 - 最后,所有内容必须符合规则,否则回答无效。 回顾示例格式: - 首先,回答问题的内容。 - 然后,输出“§§相关问题§§”,紧接着是相关问题列表,如“1. ··· 2. ··· 3. ···”,没有换行分散。 所以,我的回答结构应该是: 1. 解释如何在Go中从数组获取切片。 2. 以中文回答。 3. 结束时,输出“§§相关问题§§”,然后列出3-5个相关问题。 现在,内容部分: - 在Go中,数组是固定大小的,切片是动态大小的视图。 - 要从数组获取切片,可以使用切片操作符:`array[start:end]`,其中startend是索引。 - 例如:`arr := [5]int{1,2,3,4,5}`,然后 `slice := arr[1:4]` 会得到 `[2,3,4]`。 - 解释基本概念。 确保回答简洁明了。 然后,提出相关问题: - 相关问题应该基于用户的问题我的回答。 - 用户的问题是“golang 数组切片”,所以相关问题可能涉及: 1. 切片数组的区别。 2. 如何修改切片影响原数组。 3. 切片的基本操作。 4. 性能考虑。 5. 常见错误。 列出3-5个: 1. Go语言数组切片的主要区别是什么? 2. 如何从切片创建新的切片? 3. 修改切片会影响原始数组吗?为什么? 4. 在Go中,切片的容量长度如何工作? 5. 如何避免在切片操作中出现索引越界错误? 确保问题是相关的。 最后,输出格式: - 回答内容。 - 然后,直接“§§相关问题§§”后跟问题列表,例如: §§相关问题§§ 1. ... 2. ... 3. ... 没有额外的换行。 编写回答: - 先用中文解释。 - 可能包括一个简单的代码示例,因为用户提到了“golang”,类似于示例中的Python代码。 - 示例格式有代码块,所以我应该包括一个Go代码示例。 用户问题:“golang 数组切片”,所以重点是操作。 代码示例: ```go package main import "fmt" func main() { // 定义一个数组 arr := [5]int{1, 2, 3, 4, 5} // 从数组切片 slice := arr[1:4] // 索引从1开始到4(不包括4),所以是元素2,3,4 fmt.Println(slice) // 输出: [2 3 4] } ``` 在回答中包括这个。 然后结束。</think>在 Go 语言中,数组是固定大小的序列,而切片是动态大小的视图,可以基于数组创建。要从数组获取切片,使用切片操作符 `[start:end]`,其中 `start` 是起始索引(包含),`end` 是结束索引(不包含)。索引从 0 开始,如果省略 `start` 则默认为 0,省略 `end` 则默认为数组长度。切片是引用类型,修改切片会影响原数组。 示例代码: ```go package main import "fmt" func main() { // 定义一个数组 arr := [5]int{10, 20, 30, 40, 50} // 从数组切片:索引1到3(不包含3) slice := arr[1:3] fmt.Println(slice) // 输出: [20 30] // 修改切片会影响原数组 slice[0] = 99 fmt.Println(arr) // 输出: [10 99 30 40 50] } ``` 在这个例子中,`arr[1:3]` 创建了一个包含原数组索引 1 2 元素的切片切片共享底层数组数据,因此对切片的修改会反映到原数组上。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值