感觉比数组好用,首选。
package main import ( "fmt" ) //main is the entry of the program func main() { slice1 := make([]string, 5) slice2 := make([]int, 3, 5) slice3 := []string{"Red", "Blue", "Green"} slice4 := []int{10, 20, 30} slice4[1] = 1000 slice5 := []string{9: ""} var slice6 []int slice7 := []int{10, 20, 30, 40, 50} newSlice := slice7[1:3] newSlice[1] = 35 newSlice = append(newSlice, 60) newSlice = append(newSlice, 50) source := []string{"Apple", "Orange", "Plum", "Banana", "Grape"} slice8 := source[2:3:4] for index, value := range source { fmt.Printf("Index: %d Value: %s\n", index, value) } for index :=2; index < len(source); index++ { fmt.Printf("Index: %d Value: %s\n", index, source[index]) } slice9 := [][]int{{10}, {100, 200}} slice9[0] = append(slice9[0], 20) fmt.Println(slice1, slice2, slice3, slice4, slice5, slice6, slice7, newSlice, slice8, slice9) }