切片的容量问题

MarkGaoGit · 2017-10-30 09:48:53 · 1635 次点击

谁能详细的帮我解释一下呀 找了一下午答案弄不明白

#1
更多评论
polaris
社区,需要你我一同完善!

http://docs.studygolang.com/ref/spec#Slice_expressions 看这里的说明

> a[low : high : max]
> it controls the resulting slice's capacity by setting it to max - low

#2

http://docs.studygolang.com/ref/spec#Slice_types

"The array underlying a slice may extend past the end of the slice. The capacity is a measure of that extent: it is the sum of the length of the slice and the length of the array beyond the slice; a slice of length up to that capacity can be created by slicing a new one from the original slice. The capacity of a slice a can be discovered using the built-in function cap(a)."

package main
import (
    "fmt"
)

func main(){
        s := make([]int, 4, 10)
        s1 := s[2:cap(s)]
        fmt.Println(len(s1))
}
#3