This is a creation in Article, where the information may have evolved or changed.
1. Basic types
type |
length |
Default Value |
Description |
bool |
1 |
False |
Cannot use 0, 1 instead of false, true |
Byte |
1 |
0 |
Equivalent to Uint8 |
int, UINT |
4, 8 |
0 |
Default integer type, based on platform, 32 or 64 bits |
int8, Uint8 |
1 |
0 |
-128 ~ 127,0 ~ 255 |
Int16, UInt16 |
2 |
0 |
-32768 ~ 32767,0 ~ 65535 |
Int32, UInt32 |
4 |
0 |
-(2^32/2) ~ (2^32/2-1), 0 ~ 2^32 |
Int64, UInt64 |
8 |
0 |
-(2^64/2) ~ (2^64/2-1), 0 ~ 2^64 |
Float32 |
4 |
0.0 |
|
Float64 |
8 |
0.0 |
Default floating-point number type |
Complex64 |
8 |
|
|
complex128 |
16 |
|
|
Rune |
4 |
0 |
Unicode Code Point, Int32 |
UIntPtr |
4,8 |
0 |
UINT sufficient to store pointers |
String |
|
"" |
String, default value is an empty string, NOT NULL |
Array |
|
|
Array |
struct |
|
|
Structural body |
function |
|
Nil |
Function |
Interface |
|
Nil |
Interface |
Map |
|
Nil |
Dictionary, reference type |
Slice |
|
Nil |
Slices, reference types |
Channel |
|
Nil |
Channel, reference type |
2. Type aliases
Setting the type alias can make the code more readable, and at a glance what this variable does:
type ( int64)
3. Declaration and assignment of variables
- Declaration of variable: var a int
- Assignment of a variable: a = 123
- Declare and assign a value: var a int = 123 (if the type is omitted, the compiler automatically infers the type based on the value)
- Declaration of multiple variables
Parallel mode (can omit type, deduced by value)
varint123
The shorthand form for declaring variables inside a function:
funciont main(){ 1}
The return value can be ignored with "_" When the function returns multiple values
134
4. Type conversion
- There is no implicit conversion in go, all type conversions must show a declaration
- Conversions can only occur between two mutually compatible types
- Format for type conversions:
varfloat321.1int(a)
If the target of the transformation is a pointer, a one-way channel, or a function type that does not have a return value, then parentheses must be used to avoid parsing a syntax error.
fun main() { 100 p := *int(&x) //错误:cannot convert &x (type *int) to type int // invalid indirect of int(&x) (type int) fmt.Println(p)}
The correct approach is to use parentheses so that the compiler will *int parse the pointer type.
(*int)(p)(<-chanint)(c)(func())(x)