1. The array (array)
- Definition: An array is a fixed-length sequence of items of the same data type
- Declaration: The array declaration needs to specify the type of the constituent elements and the number of stored elements (length). After the array declaration, the length cannot be modified
- [length]type{item1, item2} // Specify the quantity
- […]. Type {item1, item2} // Any quantity
- [length]type{index1:item1, index3:item3} // Initialize the array at the specified position
Func arrayCase2 (){// Array of arbitrary bounds := [...] Int {1,2,3,4,5} fmt.Println(bounds) fmt.Println(len(bounds)) // specifies the subscript to initialize the array nums := [...] string{1:"php", 2:"java", Nums [0] = "c++" nums[1] = "golang" fmt.println (nums) Println(len(nums)) for index, value := range nums {fmt.Println(index, value)}}Copy the code
Multidimensional array
Func arraycase3 () {/ / define an array of the row 2 column 3 student01: = [3] [2] string {{" a ", "b"}, {" c "}, {" d "}} FMT. Println (student01)}Copy the code
2. Slice (slice)
- Definition: A slice is a variable length array consisting of a variable length sequence of data of the same type
- Components:
- The pointer points to the address of the array element that the first element in the slice only wants
- The number of length slice elements
- The total number of elements from the beginning to the end of a capacity slice
- The declaration requires the type of the element but not the length var NAMES []string
- Initialize the
- []type{v1, v2, … , vn}
- Make ([]type, len, cap) // Make ([]type, len, cap) // Make ([]type, len, cap
- Slice [start:end:cap] // Define the slice capacity end <= cap <= src_cap
- Operation section
- If slicing is based on the underlying element of the array created by the array, the data in the slicing will also change
- If the slice is changed based on the underlying data of the slice created in the array the data in the array is changed as well
- If slice 2 is created based on slice 1, slice 1 and slice 2 are mutually independent and complementary
- Append, slice = append(slice, item)
- The copy operation
- Copy (slice1, slice2) // Slice1 slices Copy slice2 slices. The number of copies is the minimum
Initializing slices
Println("\nslicecase1") slice_score01 := []int{1,3,1,2} slice_score02 := []int{} slice_score03 := make([]int, 3, 5) slice_score04 := make([]string, 1, 3) fmt.Println(slice_score01, len(slice_score01), cap(slice_score01)) fmt.Println(slice_score02, len(slice_score02), cap(slice_score02)) fmt.Println(slice_score03, Len (slice_score03), cap(slice_score03)) fpt.println (slice_score04, len(slice_score04), cap(slice_score04))} output: slicecase1 [1 3 1 2] 4 4 [] 0 0 [0 0 0] 3 5 [] 1 3Copy the code
Operation section
func slicecase2(){ fmt.Println("\nslicecase2") array_score := [...] Int {1,1,3,1,4,1,4} slice_score01 := array_score[1:4] fmt.Println("\nold") fmt.Println(array_score, len(array_score), Cap (array_score)) fmt.println (slice_score01, len(slice_score01), cap(slice_score01)) Println("\narray change") array_score[1] = 11111 fmt.Println(array_score, len(array_score), cap(array_score)) fmt.Println(slice_score01, len(slice_score01), Println("\ nSlice change") slice_score01[0] = 2222 fmt.Println(array_score, array_score, array_score) len(array_score), cap(array_score)) fmt.Println(slice_score01, len(slice_score01), cap(slice_score01)) } output: slicecase2 old [1 1 3 1 4 1 4] 7 7 [1 3 1] 3 6 array change [1 11111 3 1 4 1 4] 7 7 [11111 3 1] 3 6 slice change [1 2222 [2222 3 1] 3 6Copy the code
Slice append operation
Func slicecase_append(){fmt.println ("\nslicecase_append") nums1 := []int{1,2,3,4,5} nums2 := nums1[:] // create a new slice fmt.Printf("nums1: %v, %d, %d\n", nums1, len(nums1), cap(nums1)) fmt.Printf("nums2: %v, %d, %d\n", nums2, len(nums2), cap(nums2)) nums2 = append(nums2, 100) When the length is in the capacity range, only the length is increased, and the capacity and the underlying array remain the same. // When the length of the array exceeds the size of the array, a new underlying array is created, and the size of the array is intelligentlycalculated. (When the number of elements is less than 1024, the size of the array is doubled. Println("\nappend nums2") fmt.Printf("nums1: %v, %d, %d\n", nums1, len(nums1), cap(nums1)) fmt.Printf("nums2: %v, %d, %d\n", nums2, len(nums2), Cap (nums2)) nums2[0] = 111111 // Modify section 2 element fmt.Println("\nchange nums2") fmt.Printf("nums1: %v, %d, %d\n", nums1, len(nums1), cap(nums1)) fmt.Printf("nums2: %v, %d, %d\n", nums2, len(nums2), Cap (nums2)) nums1[0] = 22222 // Modify section 1 element ftt. Println("\nchange nums1") ftt. Printf("nums1: %v, %d, %d\n", nums1, len(nums1), cap(nums1)) fmt.Printf("nums2: %v, %d, %d\n", nums2, len(nums2), cap(nums2)) nums1 = append(nums1, Println("\ nappEnd nums1") fmt.Printf("nums1: %v, %d, %d\n", nums1, len(nums1), cap(nums1)) fmt.Printf("nums2: %v, %d, %d\n", nums2, len(nums2), cap(nums2)) } output: slicecase_append nums1: [1 2 3 4 5], 5, 5 nums2: [1 2 3 4 5], 5, 5 append nums2 nums1: [1 2 3 4 5], 5, 5 nums2: [1 2 3 4 5 100], 6, 10 change nums2 nums1: [1 2 3 4 5], 5, 5 nums2: [111111 2 3 4 5 100], 6, 10 change nums1 nums1: [22222 2 3 4 5], 5, 5 nums2: [111111 2 3 4 5 100], 6, 10 append nums1 nums1: [22222 2 3 4 5 1 2 3 4 1 4], 11, 12 nums2: [111111 2 3 4 5 100], 6, 10 [root@happy go]#Copy the code
Slice copy operation
func slice_copy(){ fmt.Println("\n slice_copy:") user01 := []string{"switch", "net", ""} user02 := []string{"a", "b", "c", "d"} user03 := []string{"1", "2", "3", "4", "5"} fmt.Printf("%q, %q, %q\n", user01, user02, user03) copy(user01, Printf("copy(user01, user02), %q, %q, %q\n", user01, user02, "copy(user01, user02), %q, %q\n", Printf("copy(user03, user02), %q, %q, %q\n", "copy(user03, user02), %q\n", user01, user02, user03) } output: slice_copy: ["switch" "net" ""], ["a" "b" "c" "d"], ["1" "2" "3" "4" "5"] copy(user01, user02), ["a" "b" "c"], ["a" "b" "c" "d"], ["1" "2" "3" "4" "5"] copy(user03, user02), ["a" "b" "c"], ["a" "b" "c" "d"], ["a" "b" "c" "d" "5"]Copy the code
3. The map is the map
- Definition: A map stores an unordered set of key value pairs
- Declaration: You need to specify the types that make up the elements key and value
- map[keyt_ype]value_type{k1:v1, k2:v2, … , kn:vn}
- Map [ktype]vtype{} // Create an empty map
- Make (map[ktype]vtype) // Create a map using the make function
- operation
- Len looks at the number of elements
- Delte Deletes the specified key of the map
- Range Traversing the map returns keys and values
func mapcase(){ config_kb := map[string]string{"ecs": "houyi", "slb": "lvs", "oss": "chiji", "tianji": "Ops1 "} // Use len to get the number of mapped elements fmt.Println(config_KB, Len (config_KB)) fmt.println (config_KB ["pangu"]) // Return null value fmt.println (config_KB ["ecs"]) // Check whether key exists, The first value is value. The second value is bool indicating whether the element exists. If so, the value is true. Otherwise false values, satus := config_KB ["pangu"] mt.Println(" config_KB [\"pangu "]", values, satus) values1, satus1 := config_kb["ecs"] fmt.Println("config_kb[\"ecs\"]", values1, Satus1) // Add config_KB ["pangu"] = "XXX" fmt.Println(config_KB) // delete delete existing keys delete(config_kb, "vpc") fmt.Println("delete vpc",config_kb) delete(config_kb, "Ecs ") fmt.println ("delete ecs", config_KB) // Traversing the map range returns two elements for the mapping key and value for key, value := range config_kb{ fmt.Println(key, value) }Copy the code
Practice counting the number of occurrences of each word
Func static_words(){// Count occurrences of each word raw_string := "https://github.com/avelino/awesome-go?utm_source=gold_browser_extension" config_word := map[string]int{} // For _, word := range raw_string{_, status := config_word[string(word)] if status{ config_word[string(word)]++ }else{ config_word[string(word)] = 0 } } fmt.Printf("%v\n", config_word) }Copy the code