Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

builtin: documentation should clarify how len() behaves #28759

pjebs opened this issue Nov 13, 2018 · 2 comments

builtin: documentation should clarify how len() behaves #28759

pjebs opened this issue Nov 13, 2018 · 2 comments


Copy link

@pjebs pjebs commented Nov 13, 2018

Currently the len( ) function returns an int. Changing it to uint has already been proposed and rejected.

Can the documentation clarify what len( ) returns when the number of items in a slice exceeds the max value of an int?

Copy link

@randall77 randall77 commented Nov 13, 2018

It isn't easy to make a slice with len > maxint.
This doesn't work:

var x [1<<63]struct{}

gives array bound is too large.

make([]struct{}, 1<<63) gives len argument too large in make([]struct {}).

And this code:

	x := []struct{}{struct{}{}}
	for {
		x = append(x, x...)

eventually dies with panic: runtime error: growslice: cap out of range.

I'm out of ideas for how to actually manufacture a slice with a length bigger than maxint (without using unsafe). And I'm pretty sure that's by design.

Copy link

@ianlancetaylor ianlancetaylor commented Nov 13, 2018

The language does not permit the number of elements in a slice to exceed the maximum value of int. (This was one of the main reasons we changed the size of int from 32 bits to 64 bits on 64-bit systems, back in the Go 1.1 release:

@golang golang locked and limited conversation to collaborators Nov 13, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
4 participants