duaevb1511 2019-06-28 11:23
浏览 12
已采纳

为什么cap不打印基础数组的长度?

I am reading the A Tour of GO, in the Slice length and capacity section, I ran the example:

package main

import "fmt"

func main() {
    s := []int{2, 3, 5, 7, 11, 13}
    printSlice(s)

    // Slice the slice to give it zero length.
    s = s[:0]
    printSlice(s)

    // Extend its length.
    s = s[:4]
    printSlice(s)

    // Drop its first two values.
    s = s[2:]
    printSlice(s)
}

func printSlice(s []int) {
    fmt.Printf("len=%d cap=%d %v
", len(s), cap(s), s)
}

When I build the app and run it, it prints like this:

len=6 cap=6 [2 3 5 7 11 13]
len=0 cap=6 []
len=4 cap=6 [2 3 5 7]
len=2 cap=4 [5 7]

In the Go docs, the cap is the length of the underlying array, why it's value is not 6?

  • 写回答

2条回答 默认 最新

  • dongre1907 2019-06-28 11:28
    关注

    In the Go docs, the cap is the length of the underlying array, why it's value is not 6?

    Because the capacity is not the size of the backing array (there may be special cases when they are equal, but in general they are not). When a slice is created by slicing a backing array, using 0 for the low index (or omitting it), and the array length for the high index (or omitting it), then yes, the capacity will be equal to the array's length.

    Spec: Length and capacity:

    The capacity of a slice is the number of elements for which there is space allocated in the underlying array.

    So the capacity starts at the first element of the slice, and if that is not the same as the first element of the backing array, they won't be equal. This is also stated explicitly in the Tour page you linked:

    The capacity of a slice is the number of elements in the underlying array, counting from the first element in the slice.

    There is also a full slice expression which has the form of:

    a[low : high : max]
    

    Where you can control the resulting slice's capacity, you can limit how far future slicing may extend the slice. The max index may point to an element before the last element of the array.

    See this example:

    a := [10]int{}
    
    s := a[:] // len=10, cap=10
    fmt.Printf("len=%d, cap=%d
    ", len(s), cap(s))
    
    s = a[2:] // len=8, cap=8
    fmt.Printf("len=%d, cap=%d
    ", len(s), cap(s))
    
    s = a[2:7] // len=5, cap=8
    fmt.Printf("len=%d, cap=%d
    ", len(s), cap(s))
    
    s = a[2:7:8] // len=5, cap=6
    fmt.Printf("len=%d, cap=%d
    ", len(s), cap(s))
    

    Outputs (try it on the Go Playground):

    len=10, cap=10
    len=8, cap=8
    len=5, cap=8
    len=5, cap=6
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line