Code Demo
PackageMainImport "FMT"funcMain () {s: = Make([]string,3) FMT. Println ("EMP:", s) s[0] ="a"s[1] ="B"s[2] ="C"Fmt. Println ("Set:", s) fmt. Println ("Get:", s[2]) FMT. Println ("Len:",Len(s)) s =Append(S,"D") s =Append(S,"E","F") FMT. Println ("APD:", s) c: = Make([]string,Len(s))Copy(c, s) fmt. Println ("cpy:", c) L: = s[2:5] FMT. Println ("SL1:", l) L = s[:5] FMT. Println ("SL2:", l) L = s[2:] FMT. Println ("SL3:", l) T: = []string{"G","H","I"} FMT. Println ("DCL:", t) Twod: = Make([][]int,3) forI: =0; I <3; i++ {Innerlen: = i +1Twod[i] = Make([]int, Innerlen) forJ: =0; J < Innerlen; J + + {Twod[i][j] = i + j}} fmt. Println ("2d:", Twod)}
Code Run Results
EMP: []
Set: [a B c]
Get:c
Len:3
APD: [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]]
Code interpretation:
- Sharding is a key data type in the go language and can provide a more useful sequence interface than an array
- Unlike arrays, the types of shards are defined only by the elements they contain. Create an empty shard with built-in method make must be of length
- Elements of set and get shards can be like arrays
- Use the method Len to return the length of the Shard
- In the basic operation of a Shard, the Shard supports an extra array of methods. One is built-in method append, you can append elements to the Shard
- Shards can also be copied
- Shard Support slicing operation, syntax Slice[low:high]
- For a shard, we can declare it and initialize the element content, and do it in one line
- Shards can also have multidimensional structures, and inner shards are mutable, unlike multidimensional arrays
Slices shards in the 009_go language