Beginner Fundamentals

Numbers

Go has several numeric types so you can pick the right size and kind for your data.

Integer types

Signed and unsigned integers come in different sizes.

var a int8 = 127
var b int32 = 100000
var c uint = 42 // unsigned, no negatives
fmt.Println(a, b, c)

The plain int is the most common and matches the platform word size.

Floating point

var x float32 = 3.14
var y float64 = 2.718281828
fmt.Println(x, y)

float64 is the default for decimals and offers more precision.

Converting

Conversions must be explicit between numeric types.

i := 10
f := float64(i) / 3.0
fmt.Printf("%.2f\n", f) // 3.33

The math package

The math package provides common functions and constants.

import "math"

fmt.Println(math.Sqrt(16))   // 4
fmt.Println(math.Max(3, 9))  // 9
fmt.Println(math.Pi)         // 3.141592653589793
fmt.Println(math.Floor(2.7)) // 2