当前位置:网站首页>Go array and slice
Go array and slice
2022-07-05 13:12:00 【UPythonFish】
List of articles
go Arrays and slices
Array
An array is a collection of elements of the same type , Be similar to python Collection types in . however Go Mixing different types of elements is not allowed in a language , For example, an array of strings and integers .( Of course , If it is interface{} Type array , Can contain any type )
Declaration of arrays
The representation of an array is [n]T
.n
Represents the number of elements in an array ,T
Represents the type of each element . Number of elements n
It's also part of the type , In definition , The size of the array is fixed , And can't modify
func main() {
// How to define an array
var a [3]int // Define only without assignment
var a [3]int = [3]int{
4, 5, 6} // [3]int Is the data type of the array
a := [3]int{
4} // [3] 3 The size of the index group , The length of the array cannot be greater than the size of the array
a := [30]int{
28:1} // The array size is 30, The index is 28 Insert data at the location of 1, The remaining values are of value type 0 Value padding
var a = [...]int{
3, 4, 5} // Although the use of ... initialization , But the length is also fixed , Determine the length according to the value
var a =[...]int{
50:99}
fmt.Printf("%T",a) // [51]int
// Use [...] Only this writing method is supported
}
Add :
Numbers , character string , Boolean , Array ----》 Value type ( It's worth having your own 0 value , The number is 0, character string :"" Boolean :false Array : Zero value of element type )
Slicing and map ----> Reference type Zero is nil (None:python All types of null values in are None)
go Value type and reference type
Array modification and looping
func main() {
// Array through the index value and modify the value , But it cannot exceed the index value range
var a [3]int = [3]int{
4, 5, 6}
fmt.Println(a)
a[0] = 99
fmt.Println(a[0]) // 99
// Index based loop
for i:=0;i<len(a);i++{
fmt.Println(a[i])
}
for i:=len(a)-1;i>=0;i--{
fmt.Println(a[i])
}
// Iteration based loop range It's a keyword , Can return a value , One value is the index , You can return two values , The two values are index and value
for i:=range a{
fmt.Println(a[i])
}
for i,value:=range a{
fmt.Println(i,value)
}
}
Multidimensional data
Multidimensional data , Simply put, it is data set data , Follow python The list type in is the same , You can nest... Infinitely , Here is just 2 Layer as an example
var a [3][4]int=[3][4]int{
{
3,3,3,3},{
4,4,4},{
5}}
fmt.Println(a)
// loop ( Two layers of circulation )
for _,value:=range a{
for _,v:=range value{
fmt.Println(v)
}
}
section
Slicing is a convenience created by arrays 、 Flexible and powerful packaging , The slice itself doesn't have any data , They are just applications of existing arrays ( Pointer to array )
Definition of slice
1. Define an empty slice
var s []int // If there is nothing in the brackets, it is the slice endotype
2. Based on the array , Reference assignment
a:=[10]int{
1,2,3,4,5,6,7,8,9,10}
var s []int
// Reference the array from beginning to end to s Two ways of writing
s=a[0:len(a)] // Front closing back opening section
s=[:]
fmt.Printf("s The type is :%T, The value is :%v",s,s) // s The type is :[]int, The value is :[1 2 3 4 5 6 7 8 9 10]
3. Define slices and initialize
var s []int=[]int{
2,3,4}
fmt.Println(s)
fmt.Println(len(s)) // The length is 3
fmt.Println(cap(s)) // Capacity is 3
Use of slices
Use is to get and change values through index
a:=[10]int{
1,2,3,4,5,6,7,8,9,10}
var s []int
s=[:]
fmt.Println(s[0]) // 1
s[0]=999
fmt.Println(s[100]) // Compile without error , But the executive meeting , Because it exceeds the length of the slice
fmt.Println(s) // [999 2 3 4 5 6 7 8 9 10]
fmt.Println(a) // [999 2 3 4 5 6 7 8 9 10]
You will find that when the slice changes the value , The value of the underlying array will also change accordingly , Always consistent with slice preservation
Conclusion 1: Changes in slices , It will affect the underlying array to change , Empathy , Changes in the underlying array will also affect slice changes
Length and capacity of slices
Arrays have lengths , And the length cannot be changed , Slices also have length , But the length can be changed , And the slice has capacity
a:=[10]int{
1,2,3,4,5,6,7,8,9,10}
var s []int=a[0:3] // Front closing back opening section
fmt.Println(s) // [1,2,3]
// The length of the slice , Refers to how long the current slice is
fmt.Println(len(s)) // 3
// The volume of the slice : How much value can this slice save at most , Based on the underlying array
fmt.Println(cap(s)) //cap Built in functions , Calculate the capacity of the slice 10
But here's the thing , The capacity of the slice is not necessarily equal to the length of the underlying array
a:=[10]int{
1,2,3,4,5,6,7,8,9,10}
var s []int=a[3:6] // Front closing back opening section
fmt.Println(s) // [4,5,6]
fmt.Println(len(s)) // 3
fmt.Println(cap(s)) // 7
Believe in this , You can also see , The slice capacity is determined by the underlying array , The size of the capacity depends on where the slice starts cutting the array , Capacity = The length of the array - Cut the starting point ( Index value )
make function
Can pass make Function to create a slice ,make The function has three arguments
- The first is type It can also be used to create other types of data
- The second is the length of the slice
- The third is the capacity of slices
var s []int=make([]int,3,4) // Create and assign , But there is no value in it , Default 0 value ,
fmt.Println(s) // [0 0 0]
fmt.Println(len(s)) // The length is 3
fmt.Println(cap(s)) // Capacity is 4
Additional slice
Slicing can be done through append Built in functions add values
var s =make([]int,3,4)
fmt.Println(s) // [0 0 0]
fmt.Println(len(s)) //3
fmt.Println(cap(s)) //4
s=append(s,99)
fmt.Println(s) // [0 0 0 99]
fmt.Println(len(s)) //4
fmt.Println(cap(s)) //4
// When the slice has reached the maximum capacity , If you add , length +1, The capacity is doubled based on the original capacity , Automatic expansion
s=append(s,88)
fmt.Println(s)
fmt.Println(len(s)) // 5
fmt.Println(cap(s)) // 8
So here comes the question , We know from the previous study that , Slicing is based on the reference of the underlying array , How does the underlying array change after adding slices ?
a:=[10]int{
1,2,3,4,5,6,7,8,9,10}
s:=a[2:9]
fmt.Println(s)
// Slicing change , The underlying array will change
s[0]=666
fmt.Println(s) //[666 4 5 6 7 8 9]
fmt.Println(a) //[1 2 666 4 5 6 7 8 9 10]
// Array changes , The slice will also change
a[8]=999
fmt.Println(a) //[1 2 666 4 5 6 7 8 999 10]
fmt.Println(s) //[666 4 5 6 7 8 999]
The slice is appended to the critical state , Additional , Arrays are not enough , What changes will happen ( Code on )?
s=append(s,222)
fmt.Println(s) // [666 4 5 6 7 8 999 222]
// The underlying array ? It's going to change
fmt.Println(a) // [1 2 666 4 5 6 7 8 999 222]
You can see , If the underlying array is not enough , Slice and add , It will no longer affect the original array .
go The language will automatically apply for a new array , Size of the original slice capacity 2 times , And the value of the original slice , Copy to the new array , At this point, the slice is separated from the original array , The pointer points to the new array
s=append(s,333)
fmt.Println(s) //[666 4 5 6 7 8 999 222 333]
fmt.Println(len(s)) //9
fmt.Println(cap(s)) //16
Multi slice
There are multidimensional arrays in the array , Slice based on array , There are also multidimensional slices
var s [][]int
// Define and initialize
var s [][]int=[][]int{
{
1,1,1,1,1,1,1,1,1,1,1},{
2,2,},{
3,3,3,3}}
fmt.Println(s)
fmt.Println(len(s)) // 3 A slice in the inner layer counts as a value
fmt.Println(cap(s)) // 3
// adopt make initialization
var s [][]int=make([][]int,3,4)
// Slice the inner layer , Not initialized , Just use the inner slice , All slices in the inner layer should be initialized
fmt.Println(s[0]) // [] section , This slice is not initialized
// fmt.Println(s[0][0]) // Report errors
s[0]=make([]int,5,6) // Initialize the inner slice
s[0][0]=99
fmt.Println(s[0])
fmt.Println(s[0][5]) // Slice out of bounds , Although the capacity is 6, But it hasn't been used yet , You can't take
// Circular multidimensional slice ( Based on Index , Based on iteration )
var s [][]int=[][]int{
{
1,1},{
2,2},{
3,3,3,3}}
for i:=0;i<len(s);i++{
for j:=0;j<len(s[i]);j++{
fmt.Println(s[i][j])
}
}
for _,v:=range s{
for _,v1:=range v{
fmt.Println(v1)
}
}
copy section
Take a slice , Copy to another slice , The usage scenarios are as follows ( Memory optimization scheme )
var a [10000]int=[10000]int{
3,4,5}
fmt.Println(a)
s:=a[:3]
fmt.Println(s)
// Just use s, The bottom layer is based on a large array , Memory usage is very high
// When , You can put the s copy To another slice based on a small array
s1:=make([]int,3,3)
fmt.Println(s1) // [0 0 0]
copy(s1,s)
fmt.Println(s1) //[3 4 5] , I'll use it later s1 operation , Save memory
But there is also a problem , The two slices are the same length , Sure copy, What if it's not the same length ?
s1:=make([]int,5,5)
s2:=make([]int,2,2)
copy(s1,s)
copy(s2,s)
fmt.Println(s1) // [3 4 5 0 0]
fmt.Println(s2) // [3 4]
It's obvious that , If the capacity is not enough, just copy part , If the capacity exceeds, it will be zero Type data filling
边栏推荐
- 数据泄露怎么办?'华生·K'7招消灭安全威胁
- Realize the addition of all numbers between 1 and number
- 峰会回顾|保旺达-合规和安全双驱动的数据安全整体防护体系
- 程序员成长第八篇:做好测试工作
- OpenHarmony应用开发之Navigation组件详解
- Hundred days to complete the open source task of the domestic database opengauss -- openguass minimalist version 3.0.0 installation tutorial
- 同事半个月都没搞懂selenium,我半个小时就给他整明白!顺手秀了一波爬淘宝的操作[通俗易懂]
- [深度学习论文笔记]TransBTSV2: Wider Instead of Deeper Transformer for Medical Image Segmentation
- Rocky基础知识1
- Shu tianmeng map × Weiyan technology - Dream map database circle of friends + 1
猜你喜欢
一文详解ASCII码,Unicode与utf-8
Datapipeline was selected into the 2022 digital intelligence atlas and database development report of China Academy of communications and communications
SAP UI5 ObjectPageLayout 控件使用方法分享
UnicodeDecodeError: ‘utf-8‘ codec can‘t decode byte 0xe6 in position 76131: invalid continuation byt
将函数放在模块中
聊聊异步编程的 7 种实现方式
MSTP and eth trunk
解决uni-app配置页面、tabBar无效问题
Solve Unicode decodeerror: 'GBK' codec can't decode byte 0xa2 in position 107
Principle and configuration of RSTP protocol
随机推荐
Navigation property and entityset usage in SAP segw transaction code
【服务器数据恢复】某品牌服务器存储raid5数据恢复案例
Talking about fake demand from takeout order
“百度杯”CTF比赛 九月场,Web:SQL
Small case of function transfer parameters
Pandora IOT development board learning (HAL Library) - Experiment 7 window watchdog experiment (learning notes)
Developers, is cloud native database the future?
Sorry, we can't open xxxxx Docx, because there is a problem with the content (repackaging problem)
Put functions in modules
《2022年中國銀行業RPA供應商實力矩陣分析》研究報告正式啟動
My colleague didn't understand selenium for half a month, so I figured it out for him in half an hour! Easily showed a wave of operations of climbing Taobao [easy to understand]
程序员成长第八篇:做好测试工作
数据湖(七):Iceberg概念及回顾什么是数据湖
How to realize batch sending when fishing
SAP ui5 objectpagelayout control usage sharing
实现 1~number 之间,所有数字的加和
使用 jMeter 对 SAP Spartacus 进行并发性能测试
SAP SEGW 事物码里的 ABAP Editor
Didi open source Delta: AI developers can easily train natural language models
Yyds dry goods inventory # solve the real problem of famous enterprises: move the round table