Pay attention to differences go and python The variable scope of .go It's a static variable ,python It's a dynamic variable . Usually we are in python Global variables are not used in .
go Variable scope demonstration :
go
package main import "fmt" var a int = 20 // Defining global variables requires var Statement , Global variables define that you can not use func main() { // Scope of variable c := 10 // Local variables defined inside functions fmt.Println(c) println(" Global variables a", a) // Local variables can have the same name as global variables // Local variables are preferred inside functions a := "a" println(" local variable a", a) }
Running results
python Variable scope demonstration
python
a = 10 def fun(): a = "string" print(a) def fun2(): global a a = "20" print(a) if __name__ == '__main__': fun() # a = "string" print(a) # a = 10 fun2() # a = 20 print(a) # a =20
Running results :