Go language variable
The Go language variable name consists of a letter, a number, an underscore, where the first letter cannot be a number.
The general form of declaring variables is to use the var keyword:
var identifier type
Variable declaration
First, the variable type is specified, and the default value is used if the value is not assigned after the declaration.
var= value
Second, the variable type is determined by the value itself.
var= value
Third, omit Var, note: The variable on the left should not be declared, otherwise it will cause a compilation error.
: = valuevarint=tenvar=Ten:
Multi-variable Declaration
The same type as multiple variables, non-global variablesVarVname1,Vname2,Vname3 typevname1,Vname2,Vname3=V1,V2,V3Var Vname1, Vname2, Vname3 = V1, V2 v3 //and python very much like, do not need to display the claim type, automatically infer vname1,< Span class= "PLN" > Vname2, Vname3 := V1, V2, v3 //out now: = The variable on the left should not have been declared, otherwise it would cause a compilation error //this factorization keyword is commonly used to declare global variables var ( vname1 v_type1 vname2 v_type2 /span>
Value types and reference types
All basic types, such as int, float, bool, and string, are value types that use variables of these types to point directly to a value that exists in memory. When using the equal sign to assign the value = of a variable to another variable, such as: The j = i value of I is actually copied in memory, you can get the memory address of the variable I through &i, for example: 0xf840000040 (each address may be different). The value of a variable of value type is stored in the stack.
A variable of reference type R1 stores the memory address where the value of R1 resides, or where the first word in the memory address is located. This memory address is called a pointer, and the pointer is actually in a different word. A pointer to the same reference type can be multiple words in a contiguous memory address (the memory layout is contiguous), which is the most computationally efficient form of storage, or it can be scattered in memory, each of which indicates the memory address where the next word resides.
When the assignment statement r2 = R1 is used, only the reference (address) is copied. If the value of R1 is changed, then all references to the value will point to the modified content, and in this case, R2 will be affected.
Precautions
If you declare a local variable and do not use it in the same block of code, you will also get a compilation error, such as the variable A in the following example:
Package Mainimport "FMT" Func Main () { var a string = "abc" FMT. Println ("Hello, World")}
Attempting to compile this code will get error a declaredand not used. This value must be used, so use fmt. Println("Hello, World", a) will remove the error.
But global variables are allowed to be declared but not used. Multiple variables of the same type can be declared on the same line, such as:
var a, b,int
A variable can be assigned on the same line, called a parallel assignment, such as:
A, b,=5,7,"abc"
If you want to swap the values of two variables, you can simply use a, B = B, A.
The blank identifier _ is also used to discard values, such as the value 5 in: _, B = 5, 7 is discarded.
_ is actually a write-only variable, you can't get its value. This is done because you have to use all the declared variables in the Go language, but sometimes you don't need to use all the return values from a function.
Parallel assignments are also used when a function returns multiple return values, such as Val and error err, which are obtained by calling the FUNC1 function at the same time: val, err = Func1 (var1).
Go Language Constants
A constant is an identifier for a simple value that will not be modified when the program is run. The data types in constants can only be Boolean, numeric (integer, float, and plural) and string.
Definition Format for constants:
Const[type]= value
You can omit the type descriptor [type], because the compiler can infer its type based on the value of the variable.
- An explicit type definition:
const b string = "abc"
- An implicit type definition:
const b = "abc"
Multiple declarations of the same type can be abbreviated as:
Const c_name1,= value1, value2
Constants can also be used as enumerations:
Const( Unknown=0Female=1Male =2)
Constants can be used with Len (), Cap (), unsafe. The Sizeof () function evaluates the value of an expression. In a constant expression, the function must be a built-in function, otherwise compiled:
Package Mainimport "unsafe" const ( a = "abc" B = Len (a) c = unsafe. Sizeof (a)) Func main () { println (A, B, c)}
The results of the above example run as follows:
316
Go language Operators
Operator is used to perform mathematical or logical operations while the program is running. The built-in operators for the Go language are:
- Arithmetic operators
- Relational operators
- logical operators
- Bitwise operators
- Assignment operators
- Other operators
Arithmetic operators
Arithmetic operators have plus +, minus-, multiply *, except/, self-increment + +, self-subtract--
Relational operators
relational operators have equal = =, unequal! =, greater than;, less than <, greater than or equal to >=, less than or equal to <=
logical operators
Logical operators have to do with &&, or | |, not!
Bitwise operators
The bitwise operator operates on an integer in-memory bits. Bitwise operators have a bit with &, bit or |, and xor ^, left shift operation <<, right shift operator >>.
The left-shift operator "<<" is the binocular operator. The n-bit left shift is multiplied by 2 of the n-th square. Its function to the left of the "<<" operand of all the binary all left a number of bits, the "<<" to the right of the number to specify the number of bits moved, high drop, low 0.
The right-shift operator ">>" is the binocular operator. The right-shift n-bit is the n-th-squared divided by 2. The function is to shift all the binary of the left operand of ">>" to the right of several bits, and the number to the right of ">>" to specify the number of bits to move. The high position is the symbol bit.
Assignment operators
Go supports enhanced assignment operations, such as +=,-=, etc.
Other operators
Fetch address operator &
Pointer operator *
Operator Precedence
Some operators have higher precedence, and the two-tuple operator is left-to-right in the direction of operation. The following table lists all operators and their precedence, from top to bottom, from highest to lowest:
| Priority Level |
operator |
| 7 |
^ ! |
| 6 |
*/% << >> & &^ |
| 5 |
+ - | ^ |
| 4 |
= = = < <= >= > |
| 3 |
<- |
| 2 |
&& |
| 1 |
|| |
It is recommended to prioritize the overall operation of an expression by using parentheses.
Go language Introduction (ii)