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
x/blog: slices-intro: A possible "gotcha" with different result #43235
Comments
Why did you expect to see "42 554"? What do you think should change in the blog post? In general we don't use the issue trackers for questions about how Go works. Please see https://golang.org/wiki/Questions. Thanks. I'm going to lose this because I see nothing wrong here. Please comment if you disagree. |
@ianlancetaylor I think he's right. Previously I was also of the impression that it's wrong but I think @kwangh is correct. Look at: #30169 So, with this CL, But the blog reads:
So, with that CL, return So, if I understand this correctly, maybe the blog could be updated with a different example to show that "Gotcha" but not using the |
Would a simpler example be sub slicing the last 10 bytes off a multi megabyte string; that would remove the debate between the subslices Len and cap as neither can be used to reference the megabytes of prefix |
@shmsr Thank you for the extra explanation. |
@shmsr The blog post is still correct with the current garbage collector. The fact that the capacity is now restricted doesn't matter. The entire underlyng array is still pinned. |
@ianlancetaylor It's a bit late but sorry for using issues. So in short, the underlying array is in heap place, and even after the CopyDigits function returns a result, the underlying array still stays in the heap space. The capacity does not matter. Thank you. |
Yes, I get that. I didn't say that there's a problem with the underlying array getting GC'ed. I think I wasn't really clear before in explaining my thought. I just felt that the following line could be changed a little (or the example):
Because it points to an array which is not containing the entire file anymore (but a subset of that file); hence can't be resliced back even if the users want to. A slight change to the blog might make things very clear (maybe). I hope I'm clear now. I explained the same thing before (#43235 (comment)) as well. So, do you think that the following line needs some correction?
But that again is also not true every time. The blog post is totally fine is the file contains only numbers as
Then it's is a bit confusing if one looks at the implementation of |
I think the word "into" make the sentence correct. Do you have a suggestion for how to make it more clear? Thanks. |
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
yes
What operating system and processor architecture are you using (
go env
)?go env
OutputWhat did you do?
// main.go
package main
import (
"fmt"
"io/ioutil"
"regexp"
)
var digitRegexp = regexp.MustCompile("[0-9]+")
func CopyDigits(filename string) []byte {
b, _ := ioutil.ReadFile(filename)
return digitRegexp.Find(b)
}
func main() {
digits := CopyDigits("./digits")
fmt.Println(len(digits), cap(digits))
}
// digits
// random numbers with letters
12342!!!!J)))##$(*)&ASsadfsdfas132123adfsd
What did you expect to see?
42 554
What did you see instead?
5 5
https://blog.golang.org/slices-intro
As it says, "This code behaves as advertised, but the returned []byte points into an array containing the entire file." What I expected was "42 554", but "5 5".
The text was updated successfully, but these errors were encountered: