# Go语言中数组和切片笔记

```//直接赋值并指定长度
p := [6]int{1, 2, 3, 4, 5, 6}
//申明时用...相当于指定了长度
pp := [...]int{1, 2, 3, 4, 5, 6}```

```p := [6]int{1, 2, 3, 4, 5, 6}
//使用p[from:to]进行切片,这种操作会返回一个新指向数组的指针,而不会复制数组
//p[from:to]表示从from到to-1的slice元素
x := p[1:4] //引用数组的一部分,即[2,3,4]
x := p[:3]  //从下标0开始到下标2,即[1,2,3]
x := p[4:]  //下标4截取到数组结尾,即[5,6]

//使用make函数创建slice
a := make([]int, 5)    //len(a) = 5 和 cap(a) = 5
b := make([]int, 0, 5) //len(b) = 0 和 cap(b) = 5```

```package main

import (
"fmt"
"reflect"
)

func main() {
//定义一个类型为interface{}的切片
vslice := []interface{}{
[]int{},            //slice
[]int{1, 2, 3},     //slice
[]int{1, 2, 3}[:],  //切片再切还是切片
make([]int, 3, 10), //标准的slice定义方式
[3]int{1, 2, 3},    //array数组,指定长度
[...]int{1, 2, 3},  //array数组,由编译器自动计算数组长度
}

for i, v := range vslice {
rv := reflect.ValueOf(v)

fmt.Println(i, "---", rv.Kind())
}
}```

```0 --- slice
1 --- slice
2 --- slice
3 --- slice
4 --- array
5 --- array

Process finished with exit code 0```

• 数组申明需要在方括号中指定长度或者用...隐式指明长度，而且是值传递，作为参数传递会复制出一个新的数组；
• 切片不用再方括号指定长度，从底层来看，切片实际上还是用数组来管理，其结构可以抽象为ptr：指向数组的指针、len：数组实际长度、cap：数组的最大容量，有了切片，我们可以动态扩容，并替代数组进行参数传递而不会复制出新的数组。

1、对slice进行切分操作

```package main

import "fmt"

func main() {
p := make([]int, 0, 6)
p = append(p , 1, 2, 3, 4, 5)

//p is [1 2 3 4 5] len(p) is  5 cap(p) is  6
fmt.Println("p is", p, "len(p) is ", len(p), "cap(p) is ", cap(p))

//截断左边界,改变了新切片容量大小
a := p[1:]
//a is [2 3 4 5] len(a) is  4 cap(a) is  5
fmt.Println("a is", a, "len(a) is ", len(a), "cap(a) is ", cap(a))

//左边界默认0,右边界默认为len(p)
b := p[:]
//b is [1 2 3 4 5] len(b) is  5 cap(b) is  6
fmt.Println("b is", b, "len(b) is ", len(b), "cap(b) is ", cap(b))

//右边界最大为slice的容量p[:6]
c := p[:cap(p)]
//p[:6]即取p[0]~p[5],由于len(p)为5,所以最后一个是int默认值0
//c is [1 2 3 4 5 0] len(c) is  6 cap(c) is  6
fmt.Println("c is", c, "len(c) is ", len(c), "cap(c) is ", cap(c))

//由于底层指向同一个数组,所以改变b[0],c切片中c[0]也被改变
b[0] = 100
//c is [100 2 3 4 5 0]
fmt.Println("c is", c)
}```

```p is [1 2 3 4 5] len(p) is  5 cap(p) is  6
a is [2 3 4 5] len(a) is  4 cap(a) is  5
b is [1 2 3 4 5] len(b) is  5 cap(b) is  6
c is [1 2 3 4 5 0] len(c) is  6 cap(c) is  6
c is [100 2 3 4 5 0]

Process finished with exit code 0```

2、切片的扩容：

```package main

import "fmt"

func main() {
p := make([]int, 3, 5)

p[0] = 1
p[1] = 2
p[2] = 3
//p[3]=4 //这样赋值会报错：panic:runtime error:index out of range

fmt.Println(cap(p))    //5,容量够用
p = append(p, 5, 6, 7) //需要用append,append在存储空间不足时,会自动增加存储空间
fmt.Println(cap(p))    //10,存7的时候容量5不够用,扩容到原来容量的2倍
fmt.Println(p)         //[1,2,3,5,6,7]
}```

```package main

import "fmt"

func main() {

s := make([]int, 0, 10)
s = append(s, 1, 2, 3, 4, 5)
//before s = [1 2 3 4 5] cap(s) is  10 len(s) is  5
fmt.Println("before s =", s, "cap(s) is ", cap(s), "len(s) is ", len(s))
//把s扩容两倍
d := make([]int, len(s), (cap(s)+1)*2) //加1是为了防止cap(s)==0这种情况
copy(d, s)                             //使用内建函数copy复制slice
s = d

//after s = [1 2 3 4 5] cap(s) is  22 len(s) is  5
fmt.Println("after s =", s, "cap(s) is ", cap(s), "len(s) is ", len(s))
}```

3、slice 的零值

slice 的零值是 nil，一个 nil 的 slice 的len和cap是 0。

```package main

import "fmt"

func main() {
var s []int //定义一个空的指针，s==nil，len(s)=0, cap(s)=0

fmt.Println("s is ", s, "len(s) is ", len(s), "cap(s) is ", cap(s))
if nil == s {
fmt.Println("true")
}

s = make([]int, 0, 10) //分配内存
s = append(s, 1, 2, 3, 4, 5)
}```

```s is  [] len(s) is  0 cap(s) is  0
true

Process finished with exit code 0```

```package main

import "fmt"

func main() {

p := []int{1,2,3}

a := p[:2][1:][:2]

fmt.Println(a)
}```

```[2 3] len(a) is 2 cap(a) is  2

Process finished with exit code 0```

```package main

func main() {
a := make([]int, 0)
b := append(a, 1)
_ = append(a, 2)
println(b[0])
}```

```package main

func main() {
a := make([]int, 0, 10)
b := append(a, 1)
_ = append(a, 2)
println(b[0])
}```

```package main

import "fmt"

func doAppend(a []int) {
_ = append(a, 0)
}

func main() {
a := []int{1, 2, 3, 4, 5}
doAppend(a[0:2])
fmt.Println(a)
}```

```[1 2 0 4 5]

Process finished with exit code 0```

```package main

import "fmt"

func doAppend(a []int) {
_ = append(a, 0)
}

func main() {
a := []int{1, 2, 3, 4, 5}
doAppend(a[0:2:2])
fmt.Println(a)
}```

109 篇文章23 人订阅

0 条评论

## 相关文章

### Go语言编程中字符串切割方法小结

1.func Fields(s string) []string，这个函数的作用是按照1：n个空格来分割字符串最后返回的是 []string的切片 import...

4389

1302

### 深入剖析Go语言编程中switch语句的使用

switch语句可以让一个变量对反对值的列表平等进行测试。每个值被称为一个的情况(case)，变量被接通检查每个开关盒(switch case)。 在Go编程，...

3657

96016

1502

1860

1154

1.8K3

### LeetCode 832. Flipping an Image

Given a binary matrix A, we want to flip the image horizontally, then invert it,...

390

### jquery的html,text,val

.html()用为读取和修改元素的HTML标签 .text()用来读取或修改元素的纯文本内容 .val()用来读取或修改表单元素的value值。 这三个方法功能...

2215