This is a creation in Article, where the information may have evolved or changed.
Directory
-
- Types and variables
- Data type
- Type 0 Value
- Type aliases
- Variable
- Definition and assignment of variables
- Declaration of multiple variables
- Variable list
- Variable group
- Parallel declarations
- Type conversions
- Conversion of characters to numbers
Summary
numeric types, Boolean types, reference types, type 0 values, type aliases, definition variables, type inference, short declarations, type conversions, declarations of multiple variables, conversion of characters to numbers
Types and variables
Data type
Classification
- Boolean: BOOL
- Integer: Int/uint-may be 32-bit or 64-bit depending on the platform
- 8-bit integer: int8/uint8
- BYTE: Byte (uint8 alias)
- 16-bit integer: int16/uint16
- 32-bit integer: Int32/uint32
- Rune (Int32 alias)
- 64-bit integer: Int64/uint64
- Floating point: Float32/float64
- Plural: complex64/complex128
- 32 and 64-bit integers sufficient to hold pointers: uintptr-depending on the platform may be 32-bit or 64-bit
- Other types:
- Reference type:
- Slice, map, channel, interface
- Interface: interface
- Function: Func
Type 0 Value
A value of 0 is the default value that a variable is declared to be a type, a value of type 0
bool false
, a string of""
Type aliases
type ( float32 string)
Variable
Definition and assignment of variables
Variables var
are defined using keywords
//声明varint//赋值a = 123//声明的同时赋值varint = 321
Inferred type by System
var c = 321
Short Declaration variable
d:=321
Declaration of multiple variables
Variable list
//var 语句定义了一个变量的列表;跟函数的参数列表一样,类型在后面。varbool
Variable group
var( sss,bbb = 1,2)
Parallel declarations
m,n:=13,14
_ Means Ignore
a,_,c,d:=1,2,3,4
Rules
- All variables can use type inference
- Global variables can use variable groups
- Global variables cannot omit Var, that is, you cannot use a short declaration variable
- Local variables cannot use variable groups
Type conversions
varint = 42varfloat64float64(i)varuintuint(f)
A simpler way
i := 42float64uint(f)
Conversion of characters to numbers
string()
instead of converting the number 65 to "65", it becomes the character "A". If you need to switch to "65", you need to use strconv
the package Itoa()
and Atoi()
method
import"strconv"//字符与数字的转换varint = 65fmt.Println(string//Avar//65//65<nil>