This is a creation in Article, where the information may have evolved or changed.
1.0 The meaning of variables: variables or variables, refers to a number that can be changed without a fixed value. Variables are expressed as non-numeric symbols, usually in the Latin alphabet. Variables are the opposite of constants. The purpose of a variable is to generalize the way the instruction is described. If only the real value is used, the instruction can only be applied in some cases. A variable can be used as a reservation for any one of a particular kind of value.
2.0 The expression of the go language variable
2.1 Using var
Keywords is the basic way to define variables for go, unlike OC, where go puts variable types behind variable names:
// defines a variable named "VariableName " with the type "types" var variableName type
2.2 Defining multiple variables
// Three variables that define three types are "type" var vname1, vname2, Vname3 type
2.3 Defining variables and initializing values
// The variable "variableName" is initialized as "value" and the type is "type"var variableName type = value
2.4 Initialization of multiple variables at the same time
/* define three types that are "type" three variables, and they initialize the corresponding value vname1 to V1,vname2 to V2,vname3 for v3*/var vname1, Vname2, Vname3 type= v1, v2, v3
2.5 _
(underscore) is a special variable name, and any value given to it is discarded. In this example, we give the value 35
and b
discard it at the same time 34
:
the *
2.6 Private variables fast definition and initialization
/* defines three variables, each of which initializes the corresponding value vname1 to v1,vname2 for V2,vname3, and the v3 compiler automatically infers the corresponding type based on the initialized value * / Vname1, Vname2, Vname3:= v1, v2, v3
2.7 Private variables: The go language does not have a class concept and is packaged in the form of a package, whereas a private variable is a variable declared outside a function in a package.
Package Mainimport"FMT"var nameThe data type of the string//go language is declared after, and the go language is a strong type check and Swfit, no implicit type conversionsvar A, bintFunc Main () {name="Today I am very happy, although I did not look for the job, because has the progress! Hope go can support Android development early, look forward to 1.4 version! \ n"FMT. Printf ("%s", name) C:= Plus (1,1)//local variable declaration mode FMT. Printf ("1+1=%d", c)}//additionFunc Plus (aintBint)int {
method is in the form of: Method name (parameter type) return value typereturnA +B}
2.8 Local Variables: Thego language does not have the concept of a class, but encapsulates it in the form of a package, and the local variable is the variable declared within the method, which is released after the scope .
2.9 Public variables: Thego language does not have the concept of class, but encapsulates it in the form of a package, while a global variable is a variable declared outside the method, capitalized, providing a call between the outer package
3.0 Go error at compile time for declared but unused variables