当前位置:网站首页>Go language slice
Go language slice
2022-06-11 15:45:00 【Crying while learning】
Preface
section Slice, It's like an array . Also called variable length array or dynamic array . characteristic : Lengthening .
Slice Is a container of reference type , Points to an underlying array .
establish slice grammar
Normal operation
var slice_name []type
slice_name := []type{v1, v2, v3, v4}Create slices on an existing array
// Slice name := Array [ Start subscript , End subscript ] Be careful : Does not include end subscript
slice_name := array_name[start:end]func main() {
arr := [9]int{1, 2, 3, 4, 5, 6, 7, 8, 9}
slice_test1 := arr[1:4] //2-4
slice_test2 := arr[:5] //1-5
slice_test3 := arr[5:] //6-9
slice_test4 := arr[:] //1-9
fmt.Println(slice_test1)
fmt.Println(slice_test2)
fmt.Println(slice_test3)
fmt.Println(slice_test4)
}make Keyword creation slice
make Keyword is specifically used to create data of reference type .
grammar
make(type, size1, size2)The first parameter is the type slice、map、chan
The second parameter is length
The third parameter is capacity .
When the storage length exceeds the capacity , It will automatically expand .
Default storage data ,int Use 0 fill ;string Type with an empty string . Existing values can be defaulted by subscript operation .
func main() {
slice_test1 := make([]int, 3, 5)
fmt.Printf("slice_test1: %v; slice length : %v; slice Capacity : %v\n", slice_test1, len(slice_test1), cap(slice_test1))
//append Elements , exceed slice Capacity
slice_test1 = append(slice_test1, 1, 2, 3)
fmt.Println("---append after ---")
fmt.Printf("slice_test1: %v; slice length : %v; slice Capacity : %v\n", slice_test1, len(slice_test1), cap(slice_test1))
}
slice The effect of assignment on the underlying array
- Yes array Element operation , All points to array Of slice The elements of will also change .
- Yes slice When the element in , The actual time is right array To operate on the elements of .
- slice In case of capacity expansion , It actually opens up a new memory space , So the original array No change .
Concrete example
1. When the underlying array changes , All points to this array Of slice The value at the response index location also changes .
func main() {
arr := [6]int{1, 2, 3, 4, 5, 6}
slice_test1 := arr[:]
slice_test2 := arr[:3]
slice_test3 := arr[3:]
fmt.Println("slice_test1:", slice_test1)
fmt.Println("slice_test2:", slice_test2)
fmt.Println("slice_test3:", slice_test3)
arr[1] += 1
arr[4] += 1
fmt.Println("--- Yes array After element operation ---")
fmt.Println("slice_test1:", slice_test1)
fmt.Println("slice_test2:", slice_test2)
fmt.Println("slice_test3:", slice_test3)
} 
2. When operating slice Content time , For instance from slice[index] Modify the value at the index position or use append When adding a value , All of them operate on the underlying array . So the same as (1), Others point to this array Of slice The corresponding position will also change .
func main() {
arr := [6]int{1, 2, 3, 4, 5, 6}
slice_test1 := arr[:]
slice_test2 := arr[:3]
slice_test3 := arr[3:]
fmt.Println("slice_test1:", slice_test1)
fmt.Println("slice_test2:", slice_test2)
fmt.Println("slice_test3:", slice_test3)
slice_test1[1] += 1
slice_test1[4] += 1
fmt.Println("--- Yes slice After element operation ---")
fmt.Println("slice_test1:", slice_test1)
fmt.Println("slice_test2:", slice_test2)
fmt.Println("slice_test3:", slice_test3)
}
3. When using append to slice Added value , operation slice Capacity ,slice In capacity .slice Add capacity , Will open up a new memory address . The original array Copy the values in ; Add new values after capacity expansion . Therefore, the newly added value in the case of capacity expansion will not affect the original value array To operate .
therefore slice In case of capacity expansion , Only those that have been expanded slice The value in the .array It won't change , Other points to the original array Of slice The value in does not change .
func main() {
arr := [6]int{1, 2, 3, 4, 5, 6}
slice_test1 := arr[:]
slice_test2 := arr[:3]
slice_test3 := arr[3:]
fmt.Printf("slice_test1: %v; slice_test1 length : %v; slice_test1 Capacity : %v \n", slice_test1, len(slice_test1), cap(slice_test1))
fmt.Println("slice_test2:", slice_test2)
fmt.Println("slice_test3:", slice_test3)
slice_test1 = append(slice_test1, 7, 8, 9)
slice_test1[1] += 1
slice_test1[4] += 1
fmt.Println("---slice_test1 In case of capacity expansion ---")
fmt.Println(" Original array arr:", arr)
fmt.Println("slice_test1:", slice_test1)
fmt.Println("slice_test2:", slice_test2)
fmt.Println("slice_test3:", slice_test3)
}
append Insert elements
grammar
slice_name = append(slice_name, v1, v2)
slice_name = append(slice_name, slicename2...)You can put slice_name2 The values of the slice are all inserted slice_name in . however slice_name2 There must be three points behind it , Representation time slice .
func main() {
slice_test1 := []int{1, 2, 3}
slice_test2 := []int{4, 5, 6}
fmt.Println("append front slice_test1:", slice_test1)
slice_test1 = append(slice_test1, slice_test2...)
fmt.Println("appendslice_test2 after slice_test1:", slice_test1)
slice_test1 = append(slice_test1, 7, 8, 9)
fmt.Println("append After a single element slice_test1:", slice_test1)
}
section Slice Memory analysis
When checking the memory address . value type , Such as array、int etc. ,fmt.Printf(%p\n, &arr) , You want to add... Before the variable & Symbol ; Reference type , Such as slice,fmt.Printf(%p\n, slice1) , No need to add & Symbol .
func main() {
s1 := []int{1, 2, 3, 4}
fmt.Println(s1)
fmt.Printf("s1 The address of : %p; s1 The length of : %v; s1 The capacity of : %v \n", s1, len(s1), cap(s1))
s1 = append(s1, 5, 6)
fmt.Println(s1)
fmt.Printf("s1 The address of : %p; s1 The length of : %v; s1 The capacity of : %v \n", s1, len(s1), cap(s1))
}
1. Create array s1 when , First, create an underlying array . Then slice s1 Point to the underlying array . At this time, the length and capacity are the same .
2. Slices themselves do not store data , Are the underlying array storage data . So modifying is also modifying the underlying array .
3. Capacity expansion : When the underlying array is expanded , Its capacity is doubled . So use append Operation slice , In case of capacity expansion ,slice The capacity of is also multiplied .
4. Because the expansion needs to change the underlying array , So the memory address of the underlying array has changed ; If you don't need to expand , The memory address will not change .
边栏推荐
- 【愚公系列】2022年06月 .NET架构班 077-分布式中间件 ScheduleMaster加载程序集定时任务
- Don't you understand the design and principle of thread pool? Break it up and crush it. I'll teach you how to design the thread pool
- ASEMI的MOS管25N120在不同应用场景的表现
- uniapp開發微信小程序,從構建到上線
- Hard core analysis lazy single case
- openGauss数据库性能调优概述及实例分析
- DB4AI: 数据库驱动AI
- 03 _ 事务隔离:为什么你改了我还看不见?
- Interview shock 26: how to stop threads correctly?
- In June, 2019, cat teacher's report on monitoring
猜你喜欢
随机推荐
ASEMI的MOS管25N120在不同应用场景的表现
如何预测SQL语句查询时间?
MOS transistor 24n50 parameters of asemi, 24n50 package, 24n50 size
[multi thread performance tuning] what operations cause context switching?
使用Cloud DB构建APP 快速入门-快游戏篇
selenium--显示等待(中)--详解篇
将配置导出到FTP或TFTP服务器
知网被立案调查;字节跳动成立抖音集团,或在港上市;钉钉被曝裁员30%;北京人均存款超20万元 |Q资讯
Tianjin Port coke wharf hand in hand map flapping software to visually unlock the smart coke port
leetcode 120. Triangle minimum path sum
Kaixia was selected into the 2022 global top 100 innovation institutions list of Kerui Weian
硬核分析懒汉式单例
Daily blog - wechat service permission 12 matters
The third generation Pentium B70 won the C-NCAP five-star safety performance again
带你深度了解AGC云数据库
Devil cold rice # 037 devil shares the ways to become a big enterprise; Female anchor reward routine; Self discipline means freedom; Interpretation of simple interest and compound interest
【创建型模式】单例模式
Kaixia takes the lead in launching a new generation of UFS embedded flash memory devices that support Mipi m-phy v5.0
前沿科技探究之AI功能:慢SQL发现
【创建型模式】原型模式








