Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Show inferred variable type - Printf %T

  • 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.

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