Show inferred variable type - Printf %T


When we create a variable with the := assignment, Go automatically decides the type of the variable based on the initial value assigned to it. Using Printf and the %T placeholder you can print out this type.

examples/show-type/show_type.go
package main

import (
    "fmt"
)

func main() {
    name := "Foo"
    age := 42.5
    married := true
    children := 2
    really := 2i

    fmt.Printf("%T\n", name)     // string
    fmt.Printf("%T\n", age)      // float64
    fmt.Printf("%T\n", married)  // bool
    fmt.Printf("%T\n", children) // int
    fmt.Printf("%T\n", really)   // complex128
}

string
float64
bool
int
complex128