在golang func中引用var是否正确?

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

Its not "wrong" but its not good practice.

Let me expand a bit.

lets say I do this:

func main() {
  foo := 1
  go func() {
    foo = 2
    fmt.Println(foo)
  }()
}

In the case above, 2 will be printed. Now imagine there's 20+ lines between foo := 1 and foo=2. There's a high chance you would get behaviour you don't expect and you'll have a hard time tracking since the change isn't explicit. Instead you should do:

func main() {
  foo := 1
  go func(bar int) {
    bar = 2
    fmt.Println(bar)
  }(foo)
}

Now the code is explicitly changing the value of bar (which is equivalent to foo) within func(int bar). To change the value of foo in main, you do:

func main() {
  foo := 1
  go func(bar *int) {
    *bar = 2
    fmt.Println(*bar)
  }(&foo)
}