Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>A Go Slice contains three elements: data, length, and capacity.</p> <pre><code>s := make([]int, 0, 10) </code></pre> <p>The variable s is a slice of ints with a length of 0 and a capacity of 10. The built-in len() and cap() functions allow you to get the length and capacity of a slice:</p> <pre><code>len(s) == 0 cap(s) == 10 </code></pre> <p>To increase the length of a slice, simply re-slice:</p> <pre><code>s = s[0:5] // len(s) == 5 // cap(s) == 10 </code></pre> <p>To decrease the length, you can take a sub-slice:</p> <pre><code>s = s[0:1] // len(s) == 1 </code></pre> <p>There are some shorter ways to invoke make():</p> <pre><code>a := make([]int, 10) // len(a) == cap(a) == 10 b := make([]int) // len(b) == cap(b) == 0 </code></pre> <p>That's all well and good, but what if you need to increase the length of a slice beyond its capacity? To do that, you need to allocate a new slice and copy the contents of the old slice to the new one. (The function "copy" is another built-in.)</p> <pre><code>t := make([]int, len(s), 20) copy(t, s) </code></pre> <p>The <a href="http://golang.org/doc/effective_go.html#slices" rel="noreferrer">Effective Go document</a> takes this example a bit further, implementing an Append function that appends one slice to another, resizing it if necessary.</p> <p>Slices are backed by arrays; when you make() a slice of a specific capacity, an array of that capacity is allocated in the background. The slice effectively becomes a "smart pointer" to that array. If you pass that slice (or a subslice of that slice) to another function, it is passed as a pointer to that same array. This makes sub-slices very cheap to create - it's the allocation of the backing array that is expensive.</p> <p>The Go standard library includes a number of container packages - vector, for instance - that eliminate the need to manually manage slices. Use slices for speed, and more elaborate container classes for convenience. (Saying that, I still use slices for most things.)</p> <p>You may be wondering why you need to go to all this trouble. After all, a lot of languages provide dynamically resized arrays as primitives. The reason for this is tied to Go's philosophy. The language designers don't presume to know what the appropriate allocation policy is for your program; instead they give you the tools you need to build your own data structures.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload