Type inference

When declaring a variable without specifying an explicit type (either by using the := syntax or var = expression syntax), the variable's type is inferred from the value on the right hand side.

When the right hand side of the declaration is typed, the new variable is of that same type:

  1. var i int
  2. j := i // j is an int

But when the right hand side contains an untyped numeric constant, the new variable may be an int, float64, or complex128 depending on the precision of the constant:

  1. i := 42 // int
  2. f := 3.142 // float64
  3. g := 0.867 + 0.5i // complex128

Try changing the initial value of v in the example code and observe how its type is affected.

type-inference.go

  1. package main
  2. import "fmt"
  3. func main() {
  4. v := 42 // change me!
  5. fmt.Printf("v is of type %T\n", v)
  6. }