This is a creation in Article, where the information may have evolved or changed.
-
- 1 arrays
- Declaration and initialization of arrays
- Access to array members
- Comparison of arrays
- Array as function parameter
- 2 slices
- Append function
- Using append to implement special functions
4.1 Arrays
The array is seldom used in go, often using slice, which is later said
Declaration and initialization of arrays
var a [3]int //包含3个整数的数组var q [3]int = [3]int{123}var r [3]int = [3]int{12}//[...]这样的情况,数组长度由初始化的数据个数决定。q := [...]int{123} //数组长度为3
Access to array members
for i, v := range a { fmt.Printf("%d %d\n", i, v)}
For arrays, the array length is also part of the array type, so [3]int and [4]int are different array types.]
Define constants, initialize arrays by constants
type Currency int const ( USD Currency = iota EUR GBP RMB)symbol := [..."$""9""!""¥""3 ¥"
Comparison of arrays
If the array members can be compared, then the array can also be compared
a := [2]int{12}b := [...]int{12}c := [2]int{13"true false false"d := [3]int{12}fmt.Println(a == d) // compile error: cannot compare [2]int == [3]int
Array as function parameter
In the go language, an array is passed as a parameter and is passed by value, copying the entire array past, very inefficient, and for efficiency, you can pass an array of pointers
func zero(ptr *[32]byte) { forrange ptr { ptr[i] = 0 } }
4.2 Slices
A slice represents a variable-length sequence in which the elements contain the same type. The slice type is []T
represented by.
The primary function of a slice is to access the subsequence of the array.
The slice contains three members: pointer, length, capacity. The pointer points to an address in the array that represents the number of elements the slice contains, and the capacity represents the maximum number of elements the slice can contain.
Different slices can point to the same array, or even overlap.
months := [...]string{1"January"...12"December"}
The slice operator s[i:j]
creates a new slice that points to the range of elements of the series S, from the first I to the j-1 elements. This sequence s can be an array, an array pointer, or another sequence. If I is removed, the default is 0, that is, starting with the first element, if J is removed, the default is the stomach Len (s), which is the last element.
A slice contains a pointer to an array that can be modified in a function by using slices.
func reverse(s []int) { for0, len(s)-1; i < j; i, j = i+1, j-1 { s[i], s[j] = s[j], s[i] }}a := [...]int{012345"[5 4 3 2 1 0]"
Flipping an array can be done with three reverse actions
s := []int{012345}// Rotate s left by two positions.reverse(s[:2])reverse(s[2:])reverse// "[2 3 4 5 0 1]"
You can s := []int{0, 1, 2, 3, 4, 5}
implicitly create an array of reasonable length by generating a slice, and then create a slice to point to it.
Slices are not comparable.
The key requirement for the map type in the go language remains constant throughout the life cycle, but the slices are unchanged and the pointed arrays may change, so the slices cannot be key.
Nil slices have a length and capacity of 0, but there is also a non-nil length and a capacity of 0 slices []int{}
ormake([]int, 3)[3:]
Append function
var[]rune for _, r := range "Hello, 世界" { runes = append(runes, r) } fmt.Printf("%q\n", runes) // "['H''e''l''l''o'','' ''世''界'] "
Append function in the case of insufficient current capacity, will apply twice times the previous capacity, the original data copied past and then added.
Using append to implement special functions
func remove(slice []intint) []int { copy(slice[i:], slice[i+1:]) return slice[:len(slice)-1]}func main() { s := []int{5, 6, 7, 8, 9} fmt.Println(remove(s, 2// "[5 6 8 9]"}
Do not save order
DifWe don ' t need toPreserve theOrder, we can just move the Last element into theGap:func Remove (slice []int, I int) []int {slice[i] = slice[Len(Slice)-1]returnslice[:Len(Slice)-1]}func Main () {s: = []int{5,6,7,8,9} FMT. Println (remove (s),2))//"[5 6 9 8]}