๐Ÿ”ขBasic TypesLESSON

Basic Types

Go's Built-in Types

Go has a fixed set of built-in types. Unlike languages that let you ignore type sizes, Go makes every category explicit โ€” you always know exactly how much memory a value occupies.

CategoryTypes
Booleanbool
Stringstring
Integerint, int8, int16, int32, int64
Unsigned intuint, uint8, uint16, uint32, uint64, uintptr
Byte / Runebyte (alias for uint8), rune (alias for int32)
Floatfloat32, float64
Complexcomplex64, complex128

All numeric types are distinct โ€” Go will not silently widen or narrow them for you.

When to Use Which Integer

Picking the right integer type avoids subtle bugs and communicates intent to readers:

  • int โ€” the go-to for general-purpose integers. Its size matches the platform: 32-bit on 32-bit systems, 64-bit on 64-bit systems. Use this unless you have a specific reason not to.
  • int32 / int64 โ€” reach for these when you need a guaranteed width, such as when reading binary file formats or implementing network protocols where the wire format is specified.
  • byte โ€” the natural type for raw bytes. Use it for file I/O, network buffers, and anything that processes binary data.
  • rune โ€” represents a Unicode code point (a single character in the broadest sense). Use it when iterating over or processing human-readable text, since a rune correctly handles multi-byte UTF-8 characters that a byte would split.
  • Avoid uint for general use โ€” mixing signed and unsigned integers is a common source of subtle bugs (e.g., subtracting two uint values where the result should be negative silently wraps around). Only reach for unsigned types when the domain genuinely prohibits negative values and you need the extra bit of range.

Zero Values

Go guarantees that every variable is initialized. If you declare a variable without an explicit value, it receives its type's zero value โ€” there is no concept of "undefined" or uninitialized memory in Go.

Output:

Key zero values to remember:

TypeZero value
int, float64, etc.0
boolfalse
string"" (empty string โ€” never nil)
Pointers, slices, maps, channels, functionsnil

The %q verb wraps a string in double quotes, making it obvious that an empty string is truly "" rather than invisible whitespace.

Type Conversions

Go has no implicit type conversions. Every conversion must be written explicitly as T(value). This is a deliberate design choice: implicit conversions in other languages are a common source of precision loss, overflow, and surprising behavior that Go's compiler simply refuses to allow.

Contrast this with C or JavaScript, where int + float silently promotes the integer. In Go, var f float64 = i is a compile error โ€” the compiler forces you to acknowledge the conversion.

Truncation is your responsibility. Converting a float64 to int drops the fractional part without rounding:

Numeric Constants

Untyped constants in Go carry more precision than any single numeric type. The compiler gives them the type required by their context, so a single constant can behave as an int, float64, or anything else without an explicit cast.

This is why you can write const Pi = 3.14159 and use it wherever a float32 or float64 is needed without any cast โ€” the constant adapts. Named typed constants (e.g., const x int = 5) do not have this flexibility; they behave like regular typed values.

Knowledge Check

What is the zero value of a `string` variable in Go?

`byte` is an alias for which type?

Converting `int` to `float64` in Go requires: