Type conversions

Go does not do implicit type conversion. Ever. To convert between types, you call T(v) — wrapping the value v in the target type T.

i := 42
f := float64(i)
u := uint(f)

This is intentional. Implicit conversions are a common source of bugs in other languages — Go forces you to be explicit.

Try it — remove the float64 or uint conversion in the example and see what the compiler says.

package main

import (
	"fmt"
	"math"
)

func main() {
	var x, y int = 3, 4
	var f float64 = math.Sqrt(float64(x*x + y*y))
	var z uint = uint(f)
	fmt.Println(x, y, z)
}