Go array (array) & slice (slice)

Array

An array is a set of sequences of a fixed length

Array type

And not only the type of the array type storage element, but also on the length of the array and related array of different lengths are different types of
arrays can share a different type of function

func main() {
    var a [10]int
    var b [5]int
    var c [5]int32
    fmt.Printf("type a: %T,\ntype b: %T,\ntype c: %T", a, b, c)
}

Data storage

Since the array is stored in value, so that can not be directly passed to the function to change the original value, need to pass the address to modify the original value

By value

Example:

func change(a [5]int){
    fmt.Println("in change, before change", a)
    a[0] = 100
    fmt.Println("in change, after change", a)
}

func toChange() {
    a := [5]int{0, 0, 0, 0, 0}
    fmt.Println("in main, before change", a)
    change(a)
    fmt.Println("in main. after change", a)
}

Output:

in main, before change [0 0 0 0 0]
in change, before change [0 0 0 0 0]
in change, after change [100 0 0 0 0]
in main. after change [0 0 0 0 0]

main in the array does not change

Pass by reference

Example:

func change(a *[5]int){
    fmt.Println("in change, before change", *a)
    a[0] = 100
    fmt.Println("in change, after change", *a)
}

func main() {
    a := [5]int{0, 0, 0, 0, 0}
    fmt.Println("in main, before change", a)
    change(&a)
    fmt.Println("in main. after change", a)
}

Output:

in main, before change [0 0 0 0 0]
in change, before change [0 0 0 0 0]
in change, after change [100 0 0 0 0]
in main. after change [100 0 0 0 0]

main changes in the array

Array initialization

When the array is not initialized, all the elements are default values

// 标准初始化
var array [5]int = [5]int{1, 2, 3, 4, 5}

// 省略类型,会自动判断数组长度与元素类型
var array = [5]int{1, 2, 3, 4, 5}

// 省略长度,会自动判断初始化元素个数来确定长度
var array = [...]int{1, 2, 3, 4, 5}

// 指定索引初始化
var array = [5]int{0: 3, 4: 3}

slice

An array of a reference slice, a reference type
variable length slices

Slice structure

Internal storage sections corresponding to only point to the corresponding elements of the array of pointers and slice length, capacity

package main

import "fmt"

func main() {
    var array = [5]int{1, 2, 3, 4, 5}
    var slice = array[2:]
    fmt.Printf("array: %p slice: %p\n", &array[2], slice)
}

Output:

array: 0xc00001a160 slice: 0xc00001a160

Slice [2] Starting array, so the point is that array [2]

Slice definitions

slice were [] int

Slice initialization

Slice initialization when you can create your own initialize an array, can also be used maketo initialize, but makeactually create an array

// 通过数组初始化
var slice[]int = array[start: end]

// 通过 make 初始化
var slice = make([]int, len)
var slice = make([]int, len, cap)

append

append to add elements after slicing

usage

// 切片后添加元素
slice = append(slice, elems)

// 切片后添加切片
// slice... 用来将 slice 展开
slice = append(slice, slice...) 

achieve

Since the slice is an array based, but the length of the array are immutable, perform appendthe operation,
if memory space is insufficient: Slice will open up a new section of memory storage space due to the new array, each are open twice the original length of the array
if memory space enough: slice will use the original array in memory, modifying the original array elements

Open space Example:

func main() {
    var array = [10]int{}
    var slice = array[:]
    fmt.Printf("slice %p len: %d\n", slice, len(slice))
    slice = append(slice, 2)
    fmt.Printf("slice %p len: %d(should change)\n", slice, len(slice))
    slice = append(slice, slice[:len(slice) - 2]...)
    fmt.Printf("slice %p len: %d(should not change)\n", slice, len(slice))
    slice = append(slice, 1)
    fmt.Printf("slice %p len: %d(should change)\n", slice, len(slice))
}

Output:

slice 0xc00001e050 len: 10
slice 0xc0000140a0 len: 11(should change)
slice 0xc0000140a0 len: 20(should not change)
slice 0xc00008c000 len: 21(should change)

The initial length of the array 10,
appendafter an element length of 11, due to exceeding the array the maximum length, the need to open up new memory space, a length of 20, the slice at the address changes
appendafter nine elements, a length of 20, because there is no exceed the maximum length of the array, so the same point
appendafter an element length of 21, due to exceeding the maximum length of the array, so it is necessary to open up new memory space, length of 40, pointing to a slice of a change in address

Modify the array Example:

func main() {
    var array = [10]int{8: 1}
    var slice = array[: 8]
    fmt.Printf("slice %p len: %d\n", slice, len(slice))
    fmt.Printf("array: %v\n", array)
    slice = append(slice, 2)
    fmt.Printf("slice %p len: %d(should not change)\n", slice, len(slice))
    fmt.Printf("array: %v\n", array)
    slice = append(slice, 2)
    fmt.Printf("slice %p len: %d(should not change)\n", slice, len(slice))
    fmt.Printf("array: %v\n", array)
    slice = append(slice, 2)
    fmt.Printf("slice %p len: %d(should change)\n", slice, len(slice))
    fmt.Printf("array: %v\n", array)
}

Output:

slice 0xc00001e050 len: 8
array: [0 0 0 0 0 0 0 0 1 0]
slice 0xc00001e050 len: 9(should not change)
array: [0 0 0 0 0 0 0 0 2 0]
slice 0xc00001e050 len: 10(should not change)
array: [0 0 0 0 0 0 0 0 2 2]
slice 0xc0000140a0 len: 11(should change)
array: [0 0 0 0 0 0 0 0 2 2]

The initial length of the array 10, but the sliced first eight elements
when the appendelement when not more than the length of the array itself, directly modifying the original array element, if it exceeds, then open a new memory

copy

copyUsed to copy sections
if the copied elements sliced insufficient, the excess part of the same
if the copied elements exceed the target slice slice length (nothing to do with the capacity), ignore the excess
usage:

// 切片 dst 变为 切片 scr 的复制
copy(dst []Type, src []Type)

Example:

func main(){
    a := []int{1, 2, 3, 4, 5}
    b := make([]int, 3, 4)
    fmt.Printf("b: %v, a: %v\n", b, a)
    copy(b, a)
    fmt.Printf("b: %v, cap: %d, prt: %p\n", b, cap(b), b)
    b = append(b, 1)
    fmt.Printf("b: %v, cap: %d, prt: %p\n", b, cap(b), b)
    copy(b, a)
    fmt.Printf("b: %v, a: %v\n", b, a)
}

Output:

b: [0 0 0], a: [1 2 3 4 5]
b: [1 2 3], cap: 4, prt: 0xc000018140
b: [1 2 3 1], cap: 4, prt: 0xc000018140
b: [1 2 3 4], a: [1 2 3 4 5]

string

String (string) is not composed of a collection of bytes becomes
about string type <- Click to view

Guess you like

Origin www.cnblogs.com/dbf-/p/12074933.html