Go Basic Type
1. Boolean (bool)
Length: 1 bytes
value range: True,false
Note: The unavailable number represents true or false
2. integral type (int/uint)
depending on the platform Ken can be 32 or 64 bit
3. 8-bit integral type: int8/uint8
Length: 1 bytes
value range: -128~127/0~255
4. byte type: Byte (uint8 alias)
5. 16-bit integral type: int16/uint16
Length: 2 bytes
value range: -32768~32767/0~65535
6. 32-bit integral type: Int32 (Rune)/uint32
Length: 4 bytes
value range: -2^32/2~2^32/2-1/0~2^32-1
7. 64-bit integral type: Int64/uint64
Length: 8 bytes
value range: -2^64/2~2^64/2-1/0~2^64-1
8. Float type: Float32/float64
Length: 4/8 bytes
decimal digits: Accurate to 7/15 decimal digits
9. plural: complex64/complex128
Length: 8/16 bytes
32-bit or 64-bit integer type that is sufficient to hold the pointer: UIntPtr
Other value Types
array,struct,string
Reference type
Slice,map,chan
interface Type
Interface
function Type
func
0 value of type
A value of 0 is not equal to a null value, but a default value when a variable is declared as a type
Typically, the default value of a value type is 0,bool to False,string is an empty string
Type aliases
Type
BYTE int8
Text string
)
var b text
Declaration and assignment of a single variable
Variable declaration format: var variable name variable type
variable assignment format: variable name = Expression
simultaneous assignment of declarations: var variable name variable type = Expression
var a int
a=123
var b int=231
//above the format can omit the variable type, inferred by the system
var c=321
//Declaration of variables and the most abbreviated method of assignment
d:=456
Declaration and assignment of multiple variables
The declaration of a global variable can be shortened by using VAR ()
The declaration of a global variable cannot omit Var, but you can use the parallel mode
All variables can use type inference
Local variables cannot be shortened using VAR (), only in parallel
VAR (
General method
A= "Ajax"
Using parallel methods and inference
s,b=1,2
)
function body
Func main () {var a,b,c,d int =1,2,3,4 =1,2,3,4 a,b,c,d: =1,2,3,4}func main () {var a,_,c,d int =1,2,3,4 var a,b,c,d =1,2,3,4< Span style= "color: #000000;" > a,b,c,d: =1,2,3,4
Type conversions for variables
There is no implicit conversion in go, all type conversions must be explicitly declared
Conversions can only occur between 2 mutually compatible types
Type conversion format
<ValueA>[:]=<TypeOfValueA> (<ValueB>)
var a float32=1.1
B:=int (a)
The following type conversions cannot be compiled by
var c bool=true//Only true,false logic type
D:=int (c)
Definition of constants The value of the constant is determined at compile time Constants are defined in the same format as variables The right side of the equal sign must be a constant or constant expression A function in a constant expression must be a built-in function Initialization rules and enumerations for constants If you do not provide an initial value when defining a constant group (note must be a constant group), an expression that uses the upstream Using the same expression does not mean that you have the same value Iota is a constant counter, starting with 0 and automatically incrementing 1 for each of the 1 constants defined in the group. Enumeration effects can be achieved through initialization rules and iotal Iota resets to 0 for every const keyword encountered |
Package MainImport ("FMT") const a int=1Const B='A'CONST BB//Error const (text="123"length=len (text) num) const (C,D=2,3e,f//The same number must be used for an upstream expression)//define more than one constant const I,J,K=1,"2","3"Func Main () {//FMT. Println (BB); Fmt. Println (length)3FMT. PRINTLN (num)3FMT. Println (c); 2FMT. Println (d); 3FMT. Println (e); 2FMT. Println (f); 3}
Package Mainimport ("FMT")Const(A="A"B C=Iota D E=Iota F='A' //Internal Use _f)Const(G=Iota) Func main () {FMT. Println (A)//AFmt. Println (B)//AFmt. Println (C)//2Fmt. Println (D)//3Fmt. Println (E)//4Fmt. Println (f)// $Fmt. Println (G)//0}
| go are combined from left to right in priority (High-low) ^ ! (unary operator) * /% << >> & &^ +-| ^ (two-tuple operator) ==! = < <= <-(exclusively for channel) strong> && || |
func Main () {FMT. Println ( ^2 ) // -3 is now a unary operator FMT. Println (1 ^2 ) // 3 at this point, the two-tuple operator FMT. Println (! True ); // false FMT. Println (1 <<10 ); // 1024 FMT. Println (1 >>// 0 }
Package Mainimport ("FMT")/**5= 010111=10115&11=0001=1 (both 1 to 1) 5|11=1111=15 (with one 1 to 1) 5^11=1110=14 (with only 1 for 1) 5&^11=0100=4 (the second number is 1, Change the position of the first number to 0, and the result is the value of the first number changed) 4&^8=0100=401001000**/Func Main () {FMT. Println (5& One) fmt. Println (5| One) fmt. Println (5^ One) fmt. Println (5&^ One) fmt. Println (4&^8)}
Iota of constants and enumeration of <<< implementing computer storage Units
package mainimport ( " FMT ) (B float64 =1 << (Iota *10 ) KB MB GB) func main () { Fmt. Println (B) // 1 FMT. Println (KB) // 1024 FMT. Println (MB) // 1.048576e+06 FMT. Println (GB) // 1.073741824e+09 }
pointer
go while retaining pointers, unlike other programming languages, pointer operations and the "-to" operator are not supported in Go,
and directly using the "." Selector to manipulate pointers to the target object's members
operator "&" Take the variable address and use "*" to indirectly access the target object
-----------------
Package Mainimport ("fmt") Func main () {a:=1var p * int=&//0xc082002228//1}
Go basic data types and operators