您的位置:首页 > 编程语言 > Go语言

Go语言的切片slice基本操作

2017-06-30 10:20 831 查看
感觉比数组好用,首选。

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)
}


  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: