First, hopefully, the question's title makes sense (feel free to suggest edits). I have never dealt with pointers manually before, just learning Go.
So, is doing the following:
func something() {
something := *thing
go func() {
for err := range something.Errors() {
fmt.Println(err)
}
}()
}
...have any advantage over:
func something() {
go func() {
for err := range (*thing).Errors() {
fmt.Println(err)
}
}()
}
Note that I have tried doing *thing.Errors()
in second case, but it gives me error unless I put braces around it (which is understandable since Errors()
method doesn't return any pointers, I guess?).
Although this is micro-optimization that I would normally not care about, but for learning and curiosity purposes, won't the second case have more performance overhead (since its referencing pointer on every iteration)?
What is thing
, you ask? Most likely a struct, or could be an interface or could be anything (I know that certain components such as maps, strings, functions, structs etc. are passed by reference by default. But I would still like to know for those components that are not).
If Errors
is in the method set for *thing
then you can use for err := range thing.Errors() { ... }
.
Because the range expression is evaluated once at the beginning of the loop, there should be no difference in performance between the two snippets in the question and my suggestion in this answer.