1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
| package main
import ( "fmt" "sort" )
func test1() {
var a = make([]string, 5, 10) for i := 0; i < 10; i++ { a = append(a, fmt.Sprintf("%v", i)) } fmt.Println(a) }
func test2() { var a = [...]int{3, 7, 8, 9, 1} sort.Ints(a[:]) fmt.Println(a) sort.Sort(sort.Reverse(sort.IntSlice(a[:]))) fmt.Println(a) }
func main() { a := []int{1, 2, 3} a = append(a, 4) var b = []int{5, 6, 7} a = append(a, b...) fmt.Println(a) a = append(a[:1], a[2:]...) fmt.Println(a) for i := 0; i < len(a); i++ { fmt.Println(i, a[i]) } var c int if a[4] == 6 { c = a[4] } fmt.Println(c)
d := a fmt.Println(d) d[1] = 2 fmt.Println(d, a)
e := []int{0, 0} copy(e, a) fmt.Println(e) e[0] = 5 fmt.Println(e, a)
f := make([]int, 4, 6) copy(f, a) f[0] = 5 fmt.Println(f, a)
test1() test2() }
|