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

bytes: document capacity expectations of Split and Fields #47414

Open
dsnet opened this issue Jul 27, 2021 · 3 comments
Open

bytes: document capacity expectations of Split and Fields #47414

dsnet opened this issue Jul 27, 2021 · 3 comments
Labels
Documentation NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone

Comments

@dsnet
Copy link
Member

dsnet commented Jul 27, 2021

The Split function returns a slice of sub-slices from the original input. Presently, it does not document any expectations regarding the capacity of each individual sub-slice.

For example, what's the expected behavior of the following?

lines := bytes.Split([]byte("foo\nbar\n", []byte("\n"))
lines[0] = append(lines[0], "XXXXX"...)
fmt.Println(lines[1]) // does this print "bar" or "XXX"?

Currently, it prints "bar" because the logic explicitly caps the capacity to the length, which is the behavior I would expect.
However, we should probably document that this is behavior users can depend on.

@go101
Copy link

go101 commented Jul 27, 2021

similar to #43341

@go101
Copy link

go101 commented Jul 28, 2021

We could write the append line as

lines[0] = append(lines[0][:len(lines[0]):len(lines[0])], "XXXXX"...)

but it is too verbose, which might be a reason why people don't like using three-iindex slice form.
Some proposals to remove the verbosity:

@cagedmantis cagedmantis added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Jul 28, 2021
@cagedmantis cagedmantis added this to the Backlog milestone Jul 28, 2021
@cagedmantis
Copy link
Contributor

/cc @bradfitz @ianlancetaylor

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Documentation NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Projects
None yet
Development

No branches or pull requests

3 participants