(1)数组的定义
package main import ( "fmt" ) func main() { //用var定义数组可以不用赋初值 var arr1 [5]int //[0 0 0 0 0] //用冒号必须指定初值 arr2 := [3]int{1,3,5} //[1 3 5] //用“...”表示任意个数 arr3 := [...]int{2,4,6,8,10} //[2 4 6 8 10] fmt.Println(arr1,arr2,arr3) }
(2)数组的遍历
用range关键字
package main import ( "fmt" ) func main() { arr3 := [...]int{2,4,6,8,10} //[2 4 6 8 10] fmt.Println(arr3) //数组的遍历 for i := range arr3{ fmt.Println(arr3[i]) } } //结果 2 4 6 8 10
获取下标和值
package main import ( "fmt" ) func main() { arr3 := [...]int{2,4,6,8,10} //[2 4 6 8 10] fmt.Println(arr3) //数组的遍历 for i ,v:= range arr3{ fmt.Println(i,v) } } //结果 0 2 1 4 2 6 3 8 4 10
(3)数组是值类型
package main import ( "fmt" ) func printArray(arr [5]int){ for i ,v:= range arr{ fmt.Println(i,v) } } func main() { //arr2 := [3]int{1,3,5} arr3 := [...]int{2,4,6,8,10} printArray(arr3) } //结果 0 2 1 4 2 6 3 8 4 10
如果传arr2则报错
package main import ( "fmt" ) func printArray(arr [5]int){ for i ,v:= range arr{ fmt.Println(i,v) } } func main() { arr2 := [3]int{1,3,5} //arr3 := [...]int{2,4,6,8,10} printArray(arr2) //cannot use arr2 (type [3]int) as type [5]int in argument to printArray }
(1)切片
package main import "fmt" func main() { arr := [...]int{0,1,2,3,4,5,6,7} fmt.Println(arr[2:6]) //[2 3 4 5] fmt.Println(arr[:6]) //[0 1 2 3 4 5] fmt.Println(arr[2:]) //[2 3 4 5 6 7] fmt.Println(arr[:]) //[0 1 2 3 4 5 6 7] }
(2)slice的扩展
slice可以向后扩展,不可以向前扩展
package main import "fmt" func main() { arr := [...]int{0,1,2,3,4,5,6,7} s1 := arr[2:6] s2 := s1[3:5] fmt.Println(s1) //[2 3 4 5] fmt.Println(s2) //[5 6] }
s2是如何取到[5,6]的
(3)slice的实现
slice底层隐藏了三个值
s[i]不可以超越len(s),向后扩展不可以超越底层数组cap(s)
package main import "fmt" func main() { arr := [...]int{0,1,2,3,4,5,6,7} s1 := arr[2:6] s2 := s1[3:5] fmt.Println("arr = ",arr) //arr = [0 1 2 3 4 5 6 7] fmt.Printf("s1=%v,len(s1)=%d,cap(s1)=%d\n",s1,len(s1),cap(s1)) //s1=[2 3 4 5],len(s1)=4,cap(s1)=6 fmt.Printf("s2=%v,len(s2)=%d,cap(s2)=%d\n",s2,len(s2),cap(s2)) //s2=[5 6],len(s2)=2,cap(s2)=3 fmt.Println(s1[3:6]) //[5 6 7] }
(1)向slice添加元素
package main import "fmt" func main() { arr := [...]int{0,1,2,3,4,5,6,7} s1 := arr[2:6] //[2 3 4 5] s2 := s1[3:5] //[5 6] s3 := append(s2,10) s4 := append(s3,11) s5 := append(s4,12) fmt.Println(s3,s4,s5) //[5 6 10] [5 6 10 11] [5 6 10 11 12] //因为s2的cap是[5,6,7],s3 append把7替换成10,s4后超出了cap fmt.Println(arr) //[0 1 2 3 4 5 6 10] }
(2)slice的创建
package main import "fmt" func main() { //第一种 //Zero value for slice is nil var s1 []int fmt.Println(s1) //[] for i := 0; i < 5 ; i++{ s1 = append(s1,i) } fmt.Println(s1) //[0 1 2 3 4] //第二种 s2 := []int{2,4,6,8} fmt.Printf("len(s2)=%d,cap(s2)=%d\n",len(s2),cap(s2)) //len(s2)=4,cap(s2)=4 //第三种 s3 := make([]int,16) fmt.Printf("len(s3)=%d,cap(s3)=%d\n",len(s3),cap(s3)) //len(s3)=16,cap(s3)=16 //第四种 s4 := make([]int,16,32) fmt.Printf("len(s4)=%d,cap(s4)=%d\n",len(s4),cap(s4)) //len(s4)=16,cap(s4)=32 }
(3)slice的删除
package main import "fmt" func main() { s1 := []int{0,1,2,3,4,5,6} fmt.Println(s1) //[0 1 2 3 4 5 6] //删除index为3的值 s1 = append(s1[:3],s1[4:]...) fmt.Println(s1) //[0 1 2 4 5 6] }
原文:https://www.cnblogs.com/derek1184405959/p/11285729.html