Basic variable types
-
- bool
-
- signed integer: int int8 int16 int 32 int64
-
- unsigned integer: unint unit8 unit16 unit32 unit64 unitptr
-
- byte (alias for uint8)
-
- run (unicode point, alias for int32)
-
- float32 float64
- complex64 complex128
integer comparison
TypeDescriptionint unit uintptrat least 32 bit based on OS (32 bit on 32 bit OS, 64 bit on 64 bit OSint8
-128 ~ 127
($ -2^7$ ~ $2^7-1$)unit80 ~ 255
(0 ~ $2^8-1$)int16-32768 ~ 32767
unit160 ~ 65535
(if there is no any special reason, int should be used when we need a number)
- byte = unit8 (these types are to differentiate byte and number)
Get the biggest number of a type
TypeGo syntaxMeaningunint641<<64 – 12^64 – 1unint81<<8 – 12^8 – 1 = 255
How it works?
big = 1 << 100 // shift a 1 bit left 100 places <=> 2^100
small = big >> 99 // it right again 99 places <=> 2^-99, so we end up with 1<<1, or 2
Type conversion
var i int = 42
var f float = float64(i)
var u int = unint(f)
// same as
i := 42
f := float64(i)
u := uint(f)
init() function
-
- init() is called after package variable declaration
-
- each package may have multiple init()
- order of calling init() (in case of multiple init) is un-guaranteed
main() function
main() is called after all init() are run