go 切片

package main

import "fmt"

func main() {
	//创建一个初始元素个数为5的数组切片,元素初始值为0,并预留10个元素的存储空间
	a := make([]int, 5, 10)
	a[1] = 1
	fmt.Println(len(a), cap(a), a)

	b := []string{"a", "b", "c"}
	fmt.Println(len(b), cap(b), b)

	c := make([]int, 5, 10)
	c = c[1:]
	fmt.Println(len(c), cap(c), c)
	c = c[:1]
	fmt.Println(len(c), cap(c), c)
}

5 10 [0 1 0 0 0]
3 3 [a b c]
4 9 [0 0 0 0]
1 9 [0]

猜你喜欢

转载自xiangjie88.iteye.com/blog/2381974