This is a creation in Article, where the information may have evolved or changed.
Func f () (String,error) { return "test scope of variable", Nil}func main () { var name string if Name,err: = f (); n Il ==err{ println (name) } println (name) FMT. Println ("Hello, World ")}
The above example outputs:
Test scope of variable
============= (blank line)
Hello, World
Expectations are:
Test scope of variable
Test scope of variable
Hello, World
Cause Analysis:
Golang once encountered: = This symbol, if in a different scope, a new variable is defined, if in the same scope, the case is as follows: Define only variables that are not defined:
Package Mainimport "FMT" Func f () (str string) { Ok,str: = F2 () if ok{ return } return "F ()"}func F2 () (b bool,str String) { return True, "F2 ()"}func main () { str: = f () FMT. Println (str)}
Output: F2 ()