This is a creation in Article, where the information may have evolved or changed.
The Go language slice is an abstraction of an array. The words in the slices are derived from the ability to fragment intercept other arrays and slices//The length of the Go array is immutable, and in a particular scenario such collections are not very suitable,//GO provides a flexible, powerful built-in type slice ("dynamic array"),//compared to the array length of the slice is not fixed, You can append elements,//may increase the capacity of the slices when appending. Package Mainimport "FMT" Func Main () {///slice length and capacity var numbers = make ([]int, 3, 5)//Create slice Printslice (numbers)//empty (nil) slice/ /slice default is nil when uninitialized, length is 0, capacity is 0var numbers1 []intprintslice (numbers1) if numbers1 = = Nil {fmt. Printf ("Slice is empty")}//slice intercepts numbers2: = []int{0, 1, 2, 3, 4, 5, 6, 7, 8}/* prints sub-slices from index 1 (inclusive) to index 4 (not included) */fmt. Println ("numbers2[1:4] = =", Numbers2[1:4]) fmt. Println ("numbers2[:3] = =", Numbers2[:3])//index is a FMT starting from 0. Println ("numbers2[4:] = =", numbers2[4:]) Numbers3: = Make ([]int, 0, 5) printslice (NUMBERS3) NUMBERS4: = Numbers2[:2]//Index 0 (included) to index 2 (not included), this is to calculate the number of more convenient printslice (NUMBERS4)//Append () and copy () function var numbers5 []intprintslice (numbers5) numbers5 = Append (numbers5, 0)//capacity is 2 N printslice (numbers5) numbers5 = Append (numbers5, 1) printslice (numbers5) numbers5 = Append (NUMBERS5, 2, 3, 4) Printslice (NUMBERS5)//create twice-capacity slices numbers6: = make ([]int, Len (nUMBERS5), (Cap (NUMBERS5)) * *) copy (NUMBERS6, NUMBERS5)//Front is the target, followed by the content to be copied, the original overlay, Printslice (NUMBERS6)//Go language range The SLICE keyword is used for elements of the For loop that iterate over a group (array), a slice (a), A//list (channel), or a collection (map). In arrays and slices it returns the index value of the element,//returns the key value of the Key-value pair in the collection. Range is to remove the value of two nums: = []int{2, 3, 4}sum: = 0for _, num: = range nums {sum + = num}fmt. Println ("sum:", sum)//use range on the array to pass in the index and the value of two variables. In the above example, we don't need to use the ordinal of the element, so we omit it with the whitespace character "_". Sometimes we really need to know its index. For I1, NUM1: = Range Nums {if num1 = = 3 {fmt. Println ("index:", I1)//index starting from 0}}//range can also be used on map key-value pairs. KVS: = map[string]string{"A": "Apple", "B": "Banana"}for k, V: = range KVS {fmt. Printf ("%s-%s\n", K, V)}//range can also be used to enumerate Unicode strings. The first argument is the index of the character, and the second is the character (the Unicode value) itself. For I2, c: = Range "Go" {fmt. Println (I2, C)//C is the ASCII value}}func printslice (x []int) {//Here Len () is used to obtain the length of the current slice data, and the CAP () in order to find the total capacity fmt. Printf ("len=%d cap=%d slice=%v\n", Len (x), Cap (x), X)}