If I do this:
func main() {
foo := 1
go func() {
fmt.Println(foo)
}()
}
is referencing foo inside that func wrong?
If I do this:
func main() {
foo := 1
go func() {
fmt.Println(foo)
}()
}
is referencing foo inside that func wrong?
It is fine, only in changing context needs some attention(in case of local pointer variables):
package main
import (
"errors"
"fmt"
)
func test() {
defer func() { fmt.Println(1) }()
defer func() { fmt.Println(2) }()
defer func() { fmt.Println(3) }()
}
func main() {
test()
err := errors.New("error 1")
defer func() { fmt.Println(err) }()
err = errors.New("error 2")
}
and also see:
https://www.goinggo.net/2014/06/pitfalls-with-closures-in-go.html