https://play.golang.org/p/kK9c71Yt9N - This is the code I'm working off of.
I'm trying to understand lexical scoping for the variable X
. If I use the :=
operator in line 11, X
defined outside of func main
gets hidden and a new scope is getting created within the function. If I use the =
operator in the same line, the compiler complains that err
is undefined.
My understanding is that the :=
operator creates variables which are not defined and hence, only err
has to get defined. But, this understanding is clearly wrong.
What code changes can I do to make sure X
is not redefined within main()
?
I know I can do the following to make sure X
is not redefined within main()
:
var err error
X, err = InitX()
Is there a better way that I might be missing?