1. Array declaration and initialization

Declare an array of type int
var arr [5]int
arr := [5]int
Copy the code

Iterate over an array

for i:=0; i<len(arr); i++ { a[i]=0
}
for i:=0; i<len(arr); i++ { fmt.Printf("Array at index %d is %d\n",i,arr[i])
//for-range
for i,_:= range arr1 {
	arr[i]=0
}
Copy the code

New () creates an array

var arr = new([5]int)------ pointer type *[5]int
var arr [5]int//------ Value type [5]int
Copy the code

2. Array constant declaration

var arr = [5]int{1.2.3.4.5}//1, 2, 3, 4, 5
var arr = [5]int{1.2.3}//1 2 3 0 0
var arr = [5]string{2:"a".4:"b"}//_ _ a _ b (indexed)
Copy the code

3. Multidimensional arrays

package main
const (
	WIDTH  = 5
	HEIGHT = 4
)
type pixel int
var double[WIDTH][HEIGHT]pixel
func main(a) {
	for y := 0; y < HEIGHT; y++ {
		for x := 0; x < WIDTH; x++ {
			double[x][y] = 0}}}Copy the code

Slicing is a reference to a contiguous segment of an array (pointer type, never address). Slicing provides a dynamic window of related arrays. Slicing is a variable length array (more commonly).

var slice []int = arr[start:end]
var slice []int = arr[:]Var slice [] int = arr[0:len(arr)]
var slice []int = arr[2:]Arr [2:len(arr)] from the third to the last
var slice []int = arr[:3]// that is arr[0:3] from the first to the third (not including the fourth!)
Copy the code

If you want to traverse the output slice, be careful of the number after the “:”! Is the index number, but does not contain the +1 (for example, slice the entire array)

package main
import "fmt"
func main(a) {
    arr:= []byte{'g'.'o'.'l'.'a'.'n'.'g'}
    x:=arr[0:5]
    for i:=0; i<len(x); i++{ fmt.Printf("%c",x[i])   
    }
}
//output:golan
Copy the code

It does not include the sixth character! If you want full output, follow the definition exactly [0:len(arr)]

package main
import "fmt"
func main(a) {
    arr:= []byte{'g'.'o'.'l'.'a'.'n'.'g'}
    x:=arr[0:len(arr)]// x:=arr[0:6]
    for i:=0; i<len(x); i++{ fmt.Printf("%c",x[i])   
    }
}
//output:golang
Copy the code

When make () takes two arguments:

var slice []int = make([]int.10)//slice := make([]int ,10)
// 10 is len, the length of the array and the initial length of the slice
Copy the code

When make () takes three arguments:

slice := maek([]int.10.20)
//20 is the length of the entire array
// equivalent to: slice := new([20]int)[0:10]
Copy the code

Write a for-range loop as an analogy:

for(int i=0; i<strlen(arr); i++){printf("arr[%d] is %d",i,a[i]);
Copy the code

Write the for-range loop again: idex is the loop over I, and value is the index arr[I].

//
for idex,value := rangeslice { ... (specific statement inside loop)eg.fmt.Printf("arr[%d] is %d",idex,value)
}
// If only the value is needed
for _,value := rangeslice { ... (specific statement inside loop)eg.fmt.Printf("%d",value)
}
// If only the index is needed
for idex := rangeslice { ... (specific statement inside loop)eg.fmt.Printf("It`s posion is %d",idex)
}
Copy the code

7. Section recombination (reslice) The process of changing the section length is called section recombination Reslicing. The circular structure can be used to gradually fill the slice with 0 section length into the slice reslice with total capacity cap.

package main
import "fmt"
func main(a) {
	slice1 := make([]int.0.10)
	// load the slice, cap(slice1) is 10:
	for i := 0; i < cap(slice1); i++ {
		slice1 = slice1[0:i+1]
		slice1[i] = i
		fmt.Printf("The length of slice is %d\n".len(slice1))
	}

	// print the slice:
	for i := 0; i < len(slice1); i++ {
		fmt.Printf("Slice at %d is %d\n", i, slice1[i])
	}
}
Copy the code

8. Copy and append slices

package main
import "fmt"

func main(a) {
    sliceFrom := []int{1.2.3}
    sliceTo := make([]int.10)
    
    n := copy(sliceTo, sliceFrom)
    fmt.Println(sliceTo)
    fmt.Printf("Copied %d elements\n", n) // n == 3
    
    slice := []int{1.2.3}
    slice = append(slice, 4.5.6)
    fmt.Println(slice)
    }
Copy the code

9. Map type declaration initialization

var map1 map[string]int
map1 = map[string]int{"one":1."two":0}
map1["three"] =3
map1["ten"] =10
// combine the two
map2 := map[string]int{
	"one":1."two":2}
// Declare the map type with make()
map3 := make(map[string]int)
Copy the code

10. Test whether key-value pairs exist in map

package main
import "fmt"

func main(a) {
	var val int
	var isPresent bool
	map1 := make(map[string]int)
	map1["one"] =1
	map1["two"] =2
	val,isPresent = map1["two"]// If the value is not required, use _ instead
	if isPresent{
		fmt.Printf("%v! the value of two is %d",isPresent,val)
	}else{
		fmt.Printf("%v! map1 does not contain two",isPresent)
	}
} 
/ / the output: true! the value of two is 2
Copy the code

11. Element deletion

// Use the delete() function as follows
	delete(map1,"one")
	val,isPresent = map1["one"]
	if isPresent{
		fmt.Printf("%v! the value of one is %d\n",isPresent,val)
	}else{
		fmt.Printf("%v! map1 does not contain one\n",isPresent)
	}
//output:false! map1 does not contain one
Copy the code

12. For-range handles map

package main
import "fmt"

func main(a) {
	map1 := make(map[string]int)
	map1["Mon"] =1
	map1["Tue"] =2
	map1["Wed"] =3
	map1["Thu"] =4
	map1["Fri"] =5
	map1["Sat"] =6
	map1["Sun"] =7
	for key,val := range map1{
		fmt.Printf("%s is the %d of a week\n",key,val)
	}
} 
// Note: map types are unordered by default!
/*output:
Fri is the 5 of a week
Sat is the 6 of a week
Sun is the 7 of a week
Mon is the 1 of a week
Tue is the 2 of a week
Wed is the 3 of a week
Thu is the 4 of a week*/
Copy the code

13. Map slice

package main
import "fmt"

func main(a) {
	items := make([]map[int]int.5)
	for i:= range items {
		items[i] = make(map[int]int.1)
		items[i][1] = 2
	}
	fmt.Printf("Version A: Value of items: %v\n", items)
Make (map[int][]int) make(map[int][]int
Copy the code

Map [string] [int] [int] [int] [int] [int] [int] [int] We assign int to a slice value for each day of the week:

// copy to section and sort results
value[0] =1
value[1] =2
value[2] =3.// But it is difficult to find the corresponding string in map after sorting int
Copy the code

We loop through the map to determine which string corresponds to the ordered ints

package main
import "fmt"
import "sort"

func main(a) {
	map1 := make(map[string]int)
	map1["Mon"] =1
	map1["Tue"] =2
	map1["Wed"] =3
	map1["Thu"] =4
	map1["Fri"] =5
	map1["Sat"] =6
	map1["Sun"] =7
	fmt.Println("unsorted:")
	for key,val := range map1{
		fmt.Printf("%s is the %d of a week\n",key,val)
	}
	fmt.Println("sorted:")
	value := make([]int.len(map1))
	i := 0
	for _,val := range map1{
		value[i]=val
		i++
	}
	sort.Ints(value)
	for i,_ := range value{
		for j := range map1{
			if map1[j]== value[i]{
				fmt.Printf("%s is the %d of a week\n",j,value[i])			
			}
		}
	}
} 
Copy the code

The output:

unsorted:
Wed is the 3 of a week
Thu is the 4 of a week
Fri is the 5 of a week
Sat is the 6 of a week
Sun is the 7 of a week
Mon is the 1 of a week
Tue is the 2 of a week
sorted:
Mon is the 1 of a week
Tue is the 2 of a week
Wed is the 3 of a week
Thu is the 4 of a week
Fri is the 5 of a week
Sat is the 6 of a week
Sun is the 7 of a week
Copy the code

15. Map key value swap

package main
import "fmt"

func main(a) {
	map1 := make(map[string]int)
	map1["Mon"] =1
	map1["Tue"] =2
	map1["Wed"] =3
	map1["Thu"] =4
	map1["Fri"] =5
	map1["Sat"] =6
	map1["Sun"] =7
	invMap := make(map[int]string.len(map1))
	for k,v := range map1{
		invMap[v]=k
	}
	fmt.Println("inverted:")
	for k,v := range invMap{
		fmt.Printf("The %d day is %s\n",k,v)
	}
} 
Copy the code

The output:

inverted:
The 4 day is Thu
The 5 day is Fri
The 6 day is Sat
The 7 day is Sun
The 1 day is Mon
The 2 day is Tue
The 3 day is Wed
Copy the code

Of course, this time found, we use the map sorting method to sort the number, output corresponding string is also very convenient!