Type Sizes and Conversions

Go gives you fine-grained control over the size of your numeric types, though you’ll mostly stick to the defaults.

Type Sizes

The number in a type (8, 16, 32, 64) indicates how many bits are used in memory:

  • Integers: int8, int16, int32, int64, and unsigned versions uint8, uint16, etc.
  • Floats: float32, float64.
  • Defaults: int, uint, and float64 are the standard choices for most applications.

Type Conversion (Casting)

In Go, you must explicitly convert between different types. You cannot perform operations on mixed types.

var myFloat float64 = 88.26
var myInt int = int(myFloat) // Truncates the decimal: 88

When to use specific sizes?

You should only deviate from the “default” types (int, float64) when you are highly concerned about performance or memory usage in resource-constrained environments.