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

Closed
pjebs opened this issue Nov 13, 2018 · 2 comments
Closed

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

pjebs opened this issue Nov 13, 2018 · 2 comments

Comments

@pjebs
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?

@randall77

This comment has been minimized.

Copy link
Contributor

@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 {
		println(len(x))
		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.

@ianlancetaylor

This comment has been minimized.

Copy link
Contributor

@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: https://golang.org/doc/go1.1#int).

@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.
Projects
None yet
4 participants
You can’t perform that action at this time.