Go Types

Go data types

Types

bool                        true or false
int8/int16/int32/int64      n-bit integer
uint8/uint16/uint32/uint64  n-bit unsigned integer
float32/float64             n-bit floating-point number
complex64/complex128        n-bit complex number
byte                        one-byte data, identical to uint8
rune                        one character, identical to int32
uint                        uint32 or uint64
int                         int32 or int64
uintptr                     unsigned integer large enough to represent a pointer
string                      string

Define named types as follows. Values of different named types cannot be assigned directly.

type UtcTime string
type KstTime string
var t1 UtcTime = "00:00:00"
var t2 KstTime = "09:00:00"
t1 = t2 // assignment error because the types differ

Multiple types can be grouped:

type (
    UtcTime string
    KstTime string
)

Type conversion

Convert a value by writing the target type followed by parentheses.

var a1 uint16 = 1234
var a2 uint32 = uint32(a1)

Literal values

nil     special value representing absence
true    true
false   false
1234    integer
1_234   integer with ignored "_" separators
0777    octal
0o755   octal (0O is also allowed)
0x89ab  hexadecimal (0X is also allowed)
0b1111  binary (0B is also allowed)
123.4   decimal
1.23e4  floating-point number (1.23E4 is also allowed)
1.23i   complex number
"ABC"   string
'A'     character (rune)