Basic type
1. List of basic types
Copy Code code as follows:
Type length Description
BOOL 1 True/false, default false, cannot treat non 0 values as true (do not use numbers to represent True/false)
BYTE 1 uint8 Alias
Rune 4 Int32 alias. Represents a Unicode code point
Int/unit to run the platform, 32bit/64bit
Int8/uint8 1-128 ~ 127; 0 ~ 255
Int16/uint16 2-32768 ~ 32767; 0 ~ 65535
Int32/uint32 42.1 billion ~ 2.1 billion, 0 ~ 4.2 billion
Int64/uint64 8
Float32 4 Accurate to 7 decimal places, equivalent to C float
Float64 8 Accurate to 15 decimal digits, equivalent to C double
Complex64 8
complex128 16
UIntPtr is enough to hold the 32-bit, 64-bit integer of the pointer (an integral type with the pointer stored)
Array value types, arrays
struct value type, structural body
String value type, strings type, common
Slice reference type, slicing
Map reference type, dictionary
Channel reference type, channel
Interface interface type, interface
function function types, functions
2. Type conversion
Implicit type conversions are not supported, and explicit type conversions are required
Conversions occur only between two compatible types: all types of int do not allow each other to be assigned values or operations, or they will be wrong in the compile times
Copy Code code as follows:
Example
Copy Code code as follows:
Package Main
Import "FMT"
Func Main () {
A: = 0x1234
B: = 1234.56
c: = 256
Fmt. Printf ("%x\n", Uint8 (a))
Fmt. Printf ("%d\n", int (b))
Fmt. Printf ("%f\n", Float64 (c))
}
Results
Copy Code code as follows:
3. Type Alias
Copy Code code as follows:
Type T_STR string
var b t_str = "a str"
4. Type default value
Declaration does not assign a value, type 0 value, Non-null value, but the declared default value
Copy Code code as follows:
Bool:false
integers:0
floats:0.0
String: ""
Pointers,functions,interfaces,slices,channels,maps:nil
Reserved word
Copy Code code as follows:
Break Case Chan Const continue
Default defer else Fallthrough for
Func Go goto if import
Interface Map Package Range return
Select struct Switch Type var
Variable
1. Variable declaration
Copy Code code as follows:
First, specify the variable type, and if not assigned after declaration, use the default value
var v_name v_type
V_name = value
The second is to determine the type of the variable according to the value
var v_name = value
Third, omit Var, note: = The variable on the left should not be declared, or it will cause a compilation error.
V_name: = value
e.g.
var a int = 10
var B = 10
c: = 10
Example:
Copy Code code as follows:
Package Main
var a = 1234
var b string = "Hello"
var c bool
Func Main () {
println (A, B, c)
}
Results:
Copy Code code as follows:
2. Multi-Variable declaration:
Copy Code code as follows:
Type is the same as multiple variables, non-global variables
var vname1, vname2, Vname3 type
Vname1, vname2, Vname3 = v1, v2, v3
var vname1, vname2, Vname3 = v1, v2, v3//And Python very much like, do not need to display the declaration type, automatic inference
Vname1, vname2, Vname3: = V1, v2, v3//out Now: = The variable on the left should not have been declared, or it could result in a compilation error
Different types of variables, global variables, local variables cannot be used in this way
VAR (
Vname1 V_type1
Vname2 v_type2
)
Example:
Copy Code code as follows:
Package Main
var x, y int
VAR (//This can only appear in global variables, the function body does not support
a int
b BOOL
)
var c, D int = 1, 2
var e, F = 123, "Hello"
This can only appear in the body of the function without a declaration format
G, H: = 123, "Hello"
Func Main () {
G, H: = 123, "Hello"
println (x, Y, A, B, C, D, E, F, G, h)
}
Results:
Copy Code code as follows:
0 0 0 False 1 2 123 Hello 123 Hello
Attention:
A. When multivariable assignment, the value of all left variables will be evaluated before the assignment
Copy Code code as follows:
I: = 0
I, l[i] = 1, 2
Get i = 1, l[0] = 2
Sc[0], sc[0] = 1, 2
Get sc[0] = 2
B. Garbage cans _
Copy Code code as follows:
Func test () (int, string) {
Return 123, "ABC"
}
A, _: = Test ()
C. Variables that have been declared but not used will be an error in the compile phase, more stringent than Python
Constant
A constant can be a character, a string, a Boolean, or a number
Constant assignment is a compile-time behavior
1. Constant declaration
A value that can be determined at compile time and cannot be changed at runtime
Constants can be defined as numeric, Boolean, or String types
Copy Code code as follows:
Const CONSTANTNAME = value
Const Pi float32 = 3.1415926
Const C_name [Type] = value
Const C_NAME1, c_name2 = value1, value2
Const (
C_name1 = VLUAE1
C_name2 = value2
)
= Right, must be a constant or constant expression, if the function is used, must be a built-in function (compile-time behavior)
Const I = 10000
Description
Copy Code code as follows:
A. Constants must be number (Char/integer/float/complex), string, and bool that can be determined at compile time
B. When defining a constant array, if you do not provide an initialization value, it is the same as the ascending constant type, the value, exactly
Const (
A = "abc"
B
)
Then B = "abc"
C. Constants can be used with Len (), Cap (), unsafe. The Sizeof () constant evaluates the value of an expression. In a constant expression, a function must be a built-in function, otherwise the compilation is
Package Main
Import "unsafe"
Const (
A = "abc"
b = Len (a)
c = unsafe. Sizeof (a)
)
Func Main () {
println (A, B, c)
}
Result: ABC 3 16
Enumeration
Iota, a special constant, can be considered a constant that can be modified by the compiler
When each const keyword appears, it is reset to 0, and before the next const occurrence, the number represented by the iota will automatically increase by 1 each time it appears.
Does not provide an initial value, the expression using the previous line is represented
1. Statement:
Iota generates an automatic growth enumeration value starting at 0, meaning that one more enumeration value, iota+=1, whether or not to use
Basic syntax
Copy Code code as follows:
Const (
A = 1
b = 2
)
Const (
A = Iota//0
b//1
C//2
)
Const (
_ = Iota
A//1
b//2
)
Iota usage
Copy Code code as follows:
Func Main () {
Const (
A = Iota//0
b//1
C//2
D = "Ha"//Independent value, iota + = 1
E//"ha" Iota + + 1
f =//iota +=1
G//100 Iota +=1
H = Iota//7, recovery count
I//8
)
}
Const (
x = Iota//0
y = iota//1
z = Iota//2
w//omitted, default and front-like literal w = iota, i.e. 3
)
Const V = IOTA//Encountering const keyword, iota reset
Note: The number of variables per row must be consistent with const (A, B = Iota, iota C, D E, F)
Copy Code code as follows:
Func Main () {
println (A,B,C,D,E,F)
}
Results: 0 0 1 1 2 2 "respective growth"
Operator
The go operator is all left to right.
Operator overloading is not supported
Copy Code code as follows:
Precedence operator Description
High */% << >> & &^ (and not)
+ - ! ^
= =!= < <= > >=
<-channel operator
&&
Low | |
In go, + +--for statements, not expressions
Copy Code code as follows:
Package Main
Func Main () {
I: = 1
i + +
println (i)
B: = i
println (b)
Syntax error:unexpected + +, expecting semicolon or newline or}
c: = i++
means that ++/--cannot appear on the right side of the equal sign
}
Pointer
Go keeps the pointer, *t represents the type of pointer that T corresponds to
If the package name is included, it should be *. T
The symbol ' * ' representing the pointer type is always put together with the type, not next to the variable name
Pointers that also support pointers **t
1. Statement
Copy Code code as follows:
2. Notes
Copy Code code as follows:
Operator & variable address, indirect access to target object with * through pointer variable
The default value is nil, no null constants
Does not support pointer arithmetic, does not support '-> ' budget blessing, direct '. ' Selector action pointer target object member
can be in unsafe. Converting between pointer and any type of pointer
can be unsafe. Pointer is converted to UINTPTR, and then a pointer operation is made, UIntPtr can be converted to an integer
3. Example
Copy Code code as follows:
Package Main
Import "FMT"
Type User struct {
Id int
Name string
}
Func Main () {
I: = 100
var p *int = &i//Fetch address
println (*P)//value
Up: = &user{1, "Jack"}
Up. ID = 100//Direct fetch only for the thought member
Fmt. Println (UP)
U2: = *up//Copy Object
U2. Name = "Tom"
Fmt. Println (UP, U2)
}
4. Results:
Copy Code code as follows:
100
&{100 Jack}
&{100 Jack} {Tom}
Group Declaration
Copy Code code as follows:
Import (
"FMT"
"OS"
)
Const (
i = 100//The first line must have a constant expression
PI = 3.1415
)
VAR (//global variable is available, function body does not support
I int
Pi float32
)