How can a slice contain itself?

后端 未结 3 1200
花落未央
花落未央 2020-11-30 06:12

I\'m trying to learn Golang using \"The Go Programming Language\" and I\'ve reached the section on slices. They make the comparison between arrays and slices in that two arr

3条回答
  •  天命终不由人
    2020-11-30 06:50

    a slice contains a pointer to the memory holding the elements, a length for available elements count, and a capability for how big the memory. so it like:

    typedef struct { void *data; GoInt len; GoInt cap; } GoSlice;
    

    I think it is indirect, because the elements are referenced by pointer. and of course we can have the slice itself in void *data.

提交回复
热议问题