Type inference

When you use := or var without a type, Go infers the type from the right-hand side. This is type inference — less typing, same safety.

If the right-hand side is a typed value, the new variable gets that type:

var i int
j := i // j is an int

If it’s an untyped numeric constant, Go picks the type based on precision:

i := 42           // int
f := 3.142        // float64
g := 0.867 + 0.5i // complex128

Give it a shot — change the value of v in the example and watch how its inferred type changes.

package main

import "fmt"

func main() {
	v := 42 // change me!
	fmt.Printf("v is of type %T\n", v)
}