Skip to content

Capacity inconsistency for byte slices from strings #18424

Closed
@dlsniper

Description

@dlsniper

What version of Go are you using (go version)?

  • the Go Playground
  • go version go1.7.4 linux/amd64
  • go version go1.8beta2 linux/amd64

What operating system and processor architecture are you using (go env)?

  • the Go Playground

  • go 1.7:

GOARCH="amd64"
GOBIN=""
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/home/florin/go"
GORACE=""
GOROOT="/usr/local/go"
GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"
CC="gcc"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build121149425=/tmp/go-build -gno-record-gcc-switches"
CXX="g++"
CGO_ENABLED="1"
  • go 1.8 beta 2
GOARCH="amd64"
GOBIN=""
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/home/florin/go"
GORACE=""
GOROOT="/usr/local/go1.8"
GOTOOLDIR="/usr/local/go1.8/pkg/tool/linux_amd64"
CC="gcc"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build149699806=/tmp/go-build -gno-record-gcc-switches"
CXX="g++"
CGO_ENABLED="1"

What did you do?

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

What did you expect to see?

// from struct
len:	4
cap:	4
// from var
len:	4
cap:	4

What did you see instead?

  • In playground:
// from struct
len:	4
cap:	32
// from var
len:	4
cap:	32
  • go 1.7.4:
// from struct
len:    4
cap:    8
// from var
len:    4
cap:    32
  • go 1.8 beta 2:
// from struct
len:    4
cap:    8
// from var
len:    4
cap:    32

I would expect that no extra memory is allocated beyond the needs of the string but the capacity seems different in both cases. What's even more bizarre is that the values are different from between locations where this is allocated and it seems that the playground is also different from my linux machine.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions