Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Println changes capacity of a slice

Tags:

go

Consider the following code

package main

import (
    "fmt"
)

func main() {
    x := []byte("a")
    fmt.Println(x)
    fmt.Println(cap(x) == cap([]byte("a"))) // prints false

    y := []byte("a")
    fmt.Println(cap(y) == cap([]byte("a"))) // prints true

}

https://play.golang.org/p/zv8KQekaxH8

Calling simple Println with a slice variable, changes its capacity. I suspect calling any function with variadic parameters of ...interface{} produces the same effect. Is there any sane explanation for such behavior?

like image 788
vitr Avatar asked Apr 21 '18 02:04

vitr


People also ask

How can I increase my slice capacity?

The capacity of a slice is the number of elements in the underlying array, counting from the first element in the slice. The length and capacity of a slice s can be obtained using the expressions len(s) and cap(s) . You can extend a slice's length by re-slicing it, provided it has sufficient capacity.

What is the capacity of a slice in Golang?

The length of the slice is 5, which means the total number of elements present in the slice is 5 and the capacity of the slice 6 means it can store a maximum of 6 elements in it.

How do you clear a slice while keeping the allocation in memory?

Setting the slice to nil is the best way to clear a slice. nil slices in go are perfectly well behaved and setting the slice to nil will release the underlying memory to the garbage collector.

Does append return a new slice?

The append built-in function appends elements to the end of a slice. If it has sufficient capacity, the destination is resliced to accommodate the new elements. If it does not, a new underlying array will be allocated. Append returns the updated slice.


1 Answers

The explanation is, like bradfitz point in github, if you don't use make to create a slice, the compiler will use the cap it believes convenient. Creating multiple slices in different versions, or even the same, can result on slices of different capacities.

In short, if you need a concrete capacity, use make([]byte, len, cap). Otherwise you can't trust on a fixed capacity.

like image 200
Ivan Beldad Avatar answered Sep 28 '22 10:09

Ivan Beldad