Polyglot CheatSheet - Types

Last Updated: 2023-03-02




int  int8  int16  int32  int64
uint uint8 uint16 uint32 uint64 uintptr

byte // alias for uint8

rune // alias for int32
     // represents a Unicode code point

float32 float64

complex64 complex128

The int, uint, and uintptr types are usually 32 bits wide on 32-bit systems and 64 bits wide on 64-bit systems.

When you need an integer value you should use int unless you have a specific reason to use a sized or unsigned integer type.

The variable's type can be inferred from the value on the right hand side, depending on the precision of the constant.Check type:

v := 42
fmt.Printf("v is of type %T\n", v)

Type assertion: value.(string) asserts that value is a string, otherwise ok is false

if str, ok := value.(string); ok {
    return str
} else if str, ok := value.(Stringer); ok {
    return str.String()