There is oneFunction
that returns 2 values of types int
and error
. I want to assign the first value to already existing variable and assign second value to a new variable.
If I use short declaration operator :=
, there will be created 2 new variables x
and err
.
var x int
x, err := oneFunction()
To get rid of creating new x
variable I must not use :=
operator and declare err
before calling oneFunction
var x int
var err error
glob, err = oneFunction()
I'd like to know if there is another way to set first value to global variable instead of creating a new one?
No. Your example that declares var err error
is the idiomatic way to do what you want.