Variable declaration/initialization
// Three ways to declare (initialize) a single variable
//1
var name string = "hello world"
// omit the type and initialize it
var name = "hello world"
//3. Declare and initialize with the := symbol, but only inside functions
name := "hello world"
// Two ways to declare (initialize) a single pointer
//1, use &< variable name > to fetch the memory address of the variable
var age int = 10
var ptr = &age
// use the expression new(Type)
ptr := new(int)
// Two ways to declare (initialize) multiple variables
//1. Multiple variables are declared together
var (
name string
age int
gender string
)
Declare and initialize multiple variables
name, age := "hello".10
// Anonymous variables, which act as placeholders, are usually represented by underscores
// Anonymous variables do not allocate memory, do not occupy memory space, and can be declared multiple times
func GetPoint(a) (int.int) {
return 100.200
}
func main(a){
x, _ := GetPoint()
_, y := GetPoint()
fmt.Println(x, y)
}
Copy the code
2. Variable types
type | The data type | bit | note |
---|---|---|---|
Signed integer | int | 32 or 64 | Refers to the number of bits in the computer system |
int8 | 8 | ||
int16 | 16 | ||
int32 | 32 | ||
int64 | 64 | ||
Unsigned integer | uint | 32 or 64 | Refers to the number of bits in the computer system |
uint8 | 8 | ||
uint16 | 16 | ||
uint32 | 32 | ||
uint64 | 64 | ||
floating-point | float32 | 32 | Expressed as scientific notation, to the decimal point6The precision of the bit |
float64 | 64 | Expressed as scientific notation, to the decimal point15The precision of the bit | |
Byte (character type) | byte | 8 | A range, like a uint, can be used to representACSIIA character in A table, represented by single quotes, such as ‘A’ |
Rune (character) | rune | 32 | The range, like the uint32, can be used to representUnicodeA character in a table, represented by single quotes, such as’ I ‘ |
String type | string | It is related to the content and length of a specific string | Utf-8 encoding, Chinese characters occupy 3 bytes, English characters occupy 1 byte; Strings are expressed in double or back quotes, such as “hello”,hello .\n hello world , backquotes represent native strings, that is, escapes in strings are ignored |
The dictionary | map | – | Key, value Hash table of key value pairs |
Boolean type | bool | – | Cannot be converted to int |
An array of | Int [], etc | – | |
slice | Array reference | – | |
Pointer to the | * int etc. | – |