duanpa1898 2016-03-24 11:52
浏览 53

Golang的“上限”

This question already has an answer here:

The following go code:

var numbers4 = [...]int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
slice := numbers4[4:6:8] 
fmt.Printf("%d
", cap(slice))

Why is cap(slice) equal to 4?

I used to thought that should be 2.

</div>
  • 写回答

2条回答 默认 最新

  • duanjia4969 2016-03-24 12:01
    关注

    The capacity is the number of elements in the underlying array (beginning at the element referred to by the slice pointer).

    from Go Slices: usage and internals.

    Basically, it means cap != len.
    So, the slice starts at #4, and finishes at #8, hence, cap is 8 - 4 = 4.

    评论

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料