数组
package main
import "fmt"
func main() {
/*数组的声明
* 1、var ab [2]int
2、var bc
3、cd :=
[2]int{}
[3]int{1, 2}
[5]int{4: 1}
[...]int{4: 1}
[...]int{4, 1, 3}
[...]int{0: 4, 1: 1, 2: 3}
*/
var a [2]string
fmt.Println(a)
var ab [2]int = [2]int{}
fmt.Println(ab)
var bc = [2]int{1, 2}
fmt.Println(bc)
c := [3]int{}
fmt.Println(c)
b := [3]int{1, 2}
fmt.Println(b)
e := [5]int{4: 1}
fmt.Println(e)
g := [...]int{4: 1}
fmt.Println(g)
f := [...]int{4, 1, 3}
fmt.Println(f)
h := [...]int{0: 4, 1: 1, 2: 3}
fmt.Println(h)
hh := [...]int{0: 4, 1, 2}
fmt.Println(hh)
//数组指针
fmt.Println("---------数组指针----------")
i := [...]int{9: 1}
var p *[10]int = &i
fmt.Println(p)
fmt.Println(*p)
x, y := 1, 2
j := [...]*int{&x, &y}
fmt.Println(j)
k := [2]int{1, 2}
l := [2]int{1, 2}
fmt.Println(k == l)
//指向数组的指针
m := new([5]int)
fmt.Println(m)
//对某个元素进行操作
aa := [10]int{}
aa[1] = 2
fmt.Println(aa)
bb := new([10]int)
bb[1] = 2
fmt.Println(bb)
cc := [2][3]int{
{1, 1, 1},
{2, 2, 2}}
fmt.Println(cc)
dd := [...][3]int{
{1, 1, 1},
{2, 2, 2}}
fmt.Println(dd)
ff := [...][3]int{
{1, 1},
{0: 2, 2, 2}}
fmt.Println(ff)
//如下是错误的方式
// ee := [...][...]int{
// {1, 1, 1},
// {2, 2, 2}}
// fmt.Println(ee)
}
[ ]
[0 0]
[1 2]
[0 0 0]
[1 2 0]
[0 0 0 0 1]
[0 0 0 0 1]
[4 1 3]
[4 1 3]
[4 1 2]
---------数组指针----------
&[0 0 0 0 0 0 0 0 0 1]
[0 0 0 0 0 0 0 0 0 1]
[0xc0820083a0 0xc0820083a8]
true
&[0 0 0 0 0]
[0 2 0 0 0 0 0 0 0 0]
&[0 2 0 0 0 0 0 0 0 0]
[[1 1 1] [2 2 2]]
[[1 1 1] [2 2 2]]
[[1 1 0] [2 2 2]]
切片
这里写代码片
package main
import "fmt"
func main() {
/*
声明
1、a1 :=
2、var b []int
3、var d
[]int{1, 3}
[]int{}
*/
a := []int{}
fmt.Println(a)
a1 := []int{1, 3}
fmt.Println(a1)
var b []int
fmt.Println(b)
var c []int = []int{}
fmt.Println(c)
var d = []int{}
fmt.Println(d)
//含头不含尾
fmt.Println("--------截取------------")
e := [5]int{1, 2, 3, 4, 5}
f := e[2:4]
g := e[:2]
h := e[2]
i := e[2:]
fmt.Println(i)
fmt.Println(h)
fmt.Println(g)
fmt.Println(f)
//3个元素 10个内存
fmt.Println("--------make------------")
j := make([]int, 3, 10)
fmt.Println(j)
//长度,最大容量
fmt.Println(len(j), cap(j))
m := []byte{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'}
n := m[2:5]
fmt.Println(string(n))
fmt.Println(len(n), cap(n))
q := n[3:5]
fmt.Println(string(q))
//append
s1 := make([]int, 3, 6)
fmt.Printf("%p\n", s1) //内存地址
s1 = append(s1, 1, 2)
fmt.Printf("%v %p\n", s1, s1)
s1 = append(s1, 1, 2, 3)
//超过原有长度,就会重新分配内存
fmt.Printf("%v %p\n", s1, s1)
//指向的内存地址
fmt.Println("-----内存地址----")
ll := []int{1, 2, 3, 4, 5, 6}
ll1 := ll[2:5] //345
ll2 := ll[1:3] //23
fmt.Println(ll1, ll2)
ll1[0] = 9
fmt.Println(ll1, ll2)
//////////////////////////////
mm := []int{1, 2, 3, 4, 5, 6}
mm1 := mm[2:5] //345
mm2 := mm[1:3] //23
fmt.Println(mm1, mm2)
mm2 = append(mm2, 1, 2, 3, 4, 5)
mm1[0] = 9
fmt.Println(mm1, mm2)
/////////////copy///////////////////
o1 := []int{1, 2, 3, 4}
o2 := []int{5, 6}
copy(o1, o2) //o2拷贝到o1
fmt.Println(o1, o2)
oo1 := []int{1, 2, 3, 4}
oo2 := []int{5, 6}
copy(oo2, oo1) //oo1拷贝到oo2
fmt.Println(oo1, oo2)
ooo1 := []int{1, 2, 3, 4}
ooo2 := []int{5, 6, 7}
copy(ooo1[2:4], ooo2[0:2]) //oo1拷贝到oo2
fmt.Println(ooo1)
}
[]
[1 3]
[]
[]
[]
--------截取------------
[3 4 5]
3
[1 2]
[3 4]
--------make------------
[0 0 0]
3 10
cde
3 6
fg
0xc0820341b0
[0 0 0 1 2] 0xc0820341b0
[0 0 0 1 2 1 2 3] 0xc08202e180
-----内存地址----
[3 4 5] [2 3]
[9 4 5] [2 9]
[3 4 5] [2 3]
[9 4 5] [2 3 1 2 3 4 5]
[5 6 3 4] [5 6]
[1 2 3 4] [1 2]
[1 2 5 6]
有疑问加站长微信联系(非本文作者)