golang- arrays, slices, maps

1. Array

  Storing the array of the same set of data types, to be accessed through an index, the length can not be changed, the capacity equal to the length

  An array is a value type, a copy of a copy of the assignment will

  Array as a parameter, a function of the received copy is not a pointer array

. 1  Package main
 2  
. 3  Import (
 . 4     " FMT " 
. 5     " the reflect " 
. 6  )
 . 7  
. 8  FUNC main () {
 . 9     var Numbers [ . 3 ] int 
10     var numbers1 = [ 2 ] int { . 1 , 2 }              // write directly to an array of values 
. 11     numbers3: = [...] int { . 1 , 2 , . 3 , . 4 , . 5 }       //自动计算长度
12 
13    numbers[0] = 1
14    for _, i := range numbers {
15       fmt.Println(i, reflect.TypeOf(i))
16    }
17    fmt.Println(numbers[0])
18    fmt.Println(numbers)
19    fmt.Println(numbers1)
20    fmt.Println(numbers3)
21 }
View Code

2. Slice

  The bottom layer is a continuous slice of a segment of the array, slice consists of three parts: "pointer to an array," "capacity", "length" 

  Slice is more flexible than the array, you can add a slice delete copy insert automatically resize

  Identifying an array of slices and slice length

  Created: for an empty slice, slice created with values, make use you can also specify the initial length to create

. 1  Package main
 2  
. 3 Import " FMT " 
. 4  
. 5  FUNC main () {
 . 6     slice1: = [] int {}
 . 7     slice2: = [] String { " . 1 " , " 2 " , " . 3 " }
 . 8     Slice3: = the make ([] String , 3 )           // initialize the length is 3, the back can also specify the capacity is not specified by default = capacity length 
. 9     fmt.Println (slice1, slice2, Slice3)
 10 }
View Code

  Slice by slice cut out the index, although the new slice but belong to the same underlying array , changes will affect all

 1 package main
 2 
 3 import "fmt"
 4 
 5 func main() {
 6     slice1 := make([]string, 3)
 7     slice1[0], slice1[1], slice1[2] = "A", "B", "C"
 8     slice2 := slice1[:1]
 9     fmt.Println(slice1,slice2)
10     slice2[0] = "O"
11     fmt.Println(slice1,slice2)
12 }
View Code

  Slice to change the value by index

  append function to complete sections: adding and deleting sections were inserted append actually stitching parameters

    Additionally, a plurality of elements can be added

 1 package main
 2 
 3 import "fmt"
 4 
 5 func main() {
 6     slice1 := make([]string, 3)
 7     slice1[0], slice1[1], slice1[2] = "A", "B", "C"
 8     slice1 = append(slice1, "A","A","A")
 9     fmt.Println(slice1)
10 }
View Code

    delete 

 1 package main
 2 
 3 import "fmt"
 4 
 5 func main() {
 6     slice1 := make([]string, 3)
 7     slice1[0], slice1[1], slice1[2] = "A", "B", "C"
 8     slice1 = append(slice1[:1],slice1[1+1:]...)
 9     fmt.Println(slice1)
10 }
View Code

    Insertion, copy function copies the need to avoid slicing out the same operation of arrays

 1 package main
 2 
 3 import "fmt"
 4 
 5 func insert(slice1 []string, index int, val string) []string {
 6     temp_q := slice1[:index]
 7     temp_h := make([]string, len(slice1[index:]))
 8     copy(temp_h, slice1[index:])
 9     temp_q = append(temp_q, val)
10     slice1 = append(temp_q, temp_h...)
11     return slice1
12 }
13 
14 func main() {
15     slice1 := make([]string, 3)
16     slice1[0], slice1[1], slice1[2] = "A", "B", "C"
17     slice1 = insert(slice1, 1, "BH")
18     fmt.Println(slice1)
19 }
View Code

  copy function to copy complete sections 

  copy (target slice, slice source) # must make certain slice creation function, and specifies the slice length equal to the source  

 1 package main
 2 
 3 import "fmt"
 4 
 5 func main() {
 6     slice2 := []string{"1","2"}
 7     slice1 := make([]string,len(slice2))
 8     copy(slice1,slice2)
 9     fmt.Println(slice1)
10 }
View Code

   Is determined based on the slice is empty slice type is nil

3. Mapping

  go mapping is the dictionary, the hash table to achieve, you can quickly access v k through, can be seen as k: v unordered collection

  Additions and deletions to change search map

 1 package main
 2 
 3 import "fmt"
 4 
 5 func main() {
 6     dic1 := make(map[string]int)
 7     dic1["小明"] = 18
 8     dic1["小王"] = 28
 9     dic1["小李"] = 38
10     fmt.Println(dic1)
11     fmt.Println(dic1["小明"])
12     delete(dic1, "小明")
13     fmt.Println(dic1)
14 }
View Code

4. The blank check

  Mapping sections is empty 0 can be judged by whether the length nil

  Is empty array by array type, taken out [0] of the location value and zero value comparison determination of the type

. 1  Package main
 2  
. 3 Import " FMT " 
. 4  
. 5  FUNC main () {
 . 6      // var DIC1 Map [String] int          // No statement is nil var 
. 7      DIC1: = the make (Map [ String ] int )        // Short Analyzing the length may be declared 
. 8      IF DIC1 == nil || len (DIC1) == 0 {
 . 9          fmt.Println ( " Dictionary empty " )
 10      }
 . 11  }
 12 is  
13 is  
14  Package main
 15  
16 import "fmt"
17 
18 func main() {
19    //var slice1 []string
20    slice1 := []string{}
21    if slice1 == nil || len(slice1) == 0 {
22       fmt.Println("切片为空")
23    }
24 
25 }
26 
27 package main
28 
29 import "fmt"
30 
31 func main() {
32    var arr1 [5]string
33 is     IF of arr1 [ 0 ] == "" {
 34 is        fmt.Println ( " empty array " )
 35     }
 36 }
View Code

 

 

 

    

 

Guess you like

Origin www.cnblogs.com/quguanwen/p/11748864.html