This is a creation in Article, where the information may have evolved or changed.
Slices are one of the key types of the go language and provide a more powerful queue-related interface than arrays.
Package Mainimport "FMT" Func Main () {///and the array is different, the type of the slice is determined only by the elements it contains. Use the built-in function make to create a slice that is not of zero length. The following creates a slice with a length of 3, which stores the string, and//the slice element defaults to a zero value, or "" for the string. S: = make ([]string, 3) fmt. Println ("EMP:", s)//and array can use <strong>index</strong> to set element values or get element values s[0] = "a" s[1] = "B" s[2] = "C" Fmt. Println ("set:", s) fmt. Println ("Get:", s[2])//You can use the built-in function Len to get the length of the slice FMT. Println ("Len:", Len (s))//Slice also has some functionality that the array does not have. For example, we can use the built-in function append to append a value to a slice,//And then return a slice with a new slice element. Note that the APPEND function does not change the original slice, but instead generates a new slice,//We need to use the original slice to receive the new slice s = append (S, "D") s = append (S, "E", "F") fmt. Println ("APD:", s)///In addition, we can copy elements from one slice to another slice///The following example creates a new slice with the same length as the slice//and then uses the built-in copy function to copy the elements of S into C. c: = Make ([]string, Len (s)) copy (c, s) fmt. Println ("cpy:", c)//Slice also supports a take slice operation "Slice[low:high]"///gets new slice containing element "slice[low]", but does not contain "slice[high"///The following example takes a new Slices, elements include "s[2", "s[3", "s[4]". L: = S[2:5] FMT. Println ("SL1:", L)//If Low is omitted, default starting from 0, excluding "Slice[high] "element l = S[:5] FMT. Println ("SL2:", L)//If High is omitted, the default is Len (slice), including "Slice[low]" element L = s[2:] FMT. Println ("SL3:", L)//can use one line to declare and initialize a slice t: = []string{"G", "H", "I"} fmt. Println ("DCL:", T)//We can also create multidimensional slices, and arrays are different, the length of the slice element is also variable. Twod: = Make ([][]int, 3) for I: = 0; I < 3; i++ {Innerlen: = i + 1 Twod[i] = make ([]int, Innerlen) for J: = 0; J < Innerlen; J + + { TWOD[I][J] = i + j}} fmt. Println ("2d:", Twod)}
Although slices and data are different types, use FMT. PRINTLN output when the format is similar.
$ go Run slices.goemp: [ ]set: [a B C]GET:CLEN:3APD: [a b c d e f]cpy: [a b c d e F]SL1: [C D E]SL2: [a b c D e]sl3: [C D e F]DCL: [g H i]2d: [[0] [1 2] [2 3 4]]
We've learned about arrays and slices, and then we'll cover another built-in data structure for the Go language: Maps.
To learn more about slicing, see Learning Golang Language (6): Type--slice
Next example: Go by Example:maps.
English original