The definition of variable one and variable in go language
In the go language, VAR is used to define the variable, var variable name data type, and the variable will have an initial value. , the int type defaults to 0,string, which is an empty string ("") by default.
func variabledefinit () { varint; // define only unassigned initial values var string ; Fmt. Printf ("%d%q\n", a,s); // 0 ""}
And when initializing a variable, you can omit the type, and the variable gets the type from the initial value.
func variabletypeomit () { var1; var 2 ; var " ABC " ; Fmt. Println (a,b,s); // 1 2 ABC}
Second, short declaration variable
A short declaration variable can be used only within a function, and a ": =" Concise assignment statement may override the Var declaration in the case of a definite type. function cannot be used outside the: = method.
func variableshortdef () { A:1; B:2; S:"ABC"; Fmt. Println (a,b,s);}
三、一次 declaring multiple variables
A row can have multiple variables assigned, and each variable can have different data types
func variableinline () { a,b,c,s:1,2,3,"ABC"; Fmt. Println (a,b,c,s);}
Iv. Defining variables Centrally
var ( 1; 2 ; true ; " ABC " ; )
Five, local variables and global variables
Local variables: defined within a function, its scope is accessible only within the function body.
Global variables: can be accessed from anywhere within the package.
Order of query variables: Global variables, local variables
" FMT " var " Global Variables "; // declaring a global variable within a package Func Main () { fmt. Println (globalvariable);}
.
Go language variable