Skip to main content
  1. Tutorials/

Print Type of Variable in Go

··4 mins

In Go, there are several ways to check and print a variable’s type. Whether you need a quick type check during debugging or programmatic type inspection, Go provides simple and powerful tools.

What You’ll Learn:

  • Print types using fmt.Printf with %T format
  • Use reflection with reflect.TypeOf()
  • Work with modern type reflection (Go 1.22+)
  • Check types programmatically with type switches

Let’s explore each method! 🚀

The simplest way to print a variable’s type is using the %T verb in the fmt.Printf() function. This is the recommended approach for most cases.

package main

import (
    "fmt"
)

func main() {
    t1 := "text"
    t2 := []string{"apple", "strawberry", "blueberry"}
    t3 := map[string]float64{"strawberry": 3.2, "blueberry": 1.2}
    t4 := 2
    t5 := 4.5
    t6 := true

    fmt.Printf("t1: %T\n", t1)
    fmt.Printf("t2: %T\n", t2)
    fmt.Printf("t3: %T\n", t3)
    fmt.Printf("t4: %T\n", t4)
    fmt.Printf("t5: %T\n", t5)
    fmt.Printf("t6: %T\n", t6)
}

Method 2: Using reflect.TypeOf() #

You can also use the TypeOf() function from the reflect package to get type information. This is useful when you need to work with types programmatically.

package main

import (
    "fmt"
    "reflect"
)

func main() {
    t1 := "text"
    t2 := []string{"apple", "strawberry", "blueberry"}
    t3 := map[string]float64{"strawberry": 3.2, "blueberry": 1.2}
    t4 := 2
    t5 := 4.5
    t6 := true

    fmt.Printf("t1: %s\n", reflect.TypeOf(t1))
    fmt.Printf("t2: %s\n", reflect.TypeOf(t2))
    fmt.Printf("t3: %s\n", reflect.TypeOf(t3))
    fmt.Printf("t4: %s\n", reflect.TypeOf(t4))
    fmt.Printf("t5: %s\n", reflect.TypeOf(t5))
    fmt.Printf("t6: %s\n", reflect.TypeOf(t6))
}

Both methods return the same output:

t1: string
t2: []string
t3: map[string]float64
t4: int
t5: float64
t6: bool

Note: The reflect package uses runtime reflection which has some performance overhead. For simple type printing during debugging, use %T instead.

Method 3: Modern Type Reflection (Go 1.22+) #

Starting with Go 1.22, you can use reflect.TypeFor[T]() to get type information for types known at compile time. This is cleaner and more type-safe than the old pattern.

package main

import (
    "fmt"
    "reflect"
)

func main() {
    // Get type information using generics
    intType := reflect.TypeFor[int]()
    stringType := reflect.TypeFor[string]()
    sliceType := reflect.TypeFor[[]string]()
    mapType := reflect.TypeFor[map[string]int]()

    fmt.Println("int type:", intType)
    fmt.Println("string type:", stringType)
    fmt.Println("slice type:", sliceType)
    fmt.Println("map type:", mapType)

    // You can also use it with interfaces
    errorType := reflect.TypeFor[error]()
    fmt.Println("error type:", errorType)
}

Output:

int type: int
string type: string
slice type: []string
map type: map[string]int
error type: error

This replaces the old pattern of reflect.TypeOf((*SomeType)(nil)).Elem() with a much cleaner syntax.

Method 4: Type Switches and Kind() #

When you need to check types programmatically (not just print them), you can use type switches or the Kind() method.

Using Type Switches #

Type switches work with interface{} values to determine their underlying type:

package main

import "fmt"

func checkType(v interface{}) {
    switch v.(type) {
    case int:
        fmt.Println("Type: int")
    case string:
        fmt.Println("Type: string")
    case []string:
        fmt.Println("Type: []string")
    case map[string]int:
        fmt.Println("Type: map[string]int")
    default:
        fmt.Printf("Type: %T\n", v)
    }
}

func main() {
    checkType(42)
    checkType("hello")
    checkType([]string{"a", "b"})
    checkType(map[string]int{"key": 1})
    checkType(3.14)
}

Output:

Type: int
Type: string
Type: []string
Type: map[string]int
Type: float64

Using reflect.Kind() #

The Kind() method returns the specific kind of type (int, string, slice, map, etc.):

package main

import (
    "fmt"
    "reflect"
)

func main() {
    values := []interface{}{
        42,
        "hello",
        []int{1, 2, 3},
        map[string]int{"a": 1},
        true,
    }

    for _, v := range values {
        t := reflect.TypeOf(v)
        fmt.Printf("Value: %v, Type: %v, Kind: %v\n", v, t, t.Kind())
    }
}

Output:

Value: 42, Type: int, Kind: int
Value: hello, Type: string, Kind: string
Value: [1 2 3], Type: []int, Kind: slice
Value: map[a:1], Type: map[string]int, Kind: map
Value: true, Type: bool, Kind: bool

Quick Reference #

MethodUse CaseGo Version
%T with fmt.Printf()Quick debugging, simple type printingAll versions
reflect.TypeOf()Programmatic type inspectionAll versions
reflect.TypeFor[T]()Compile-time known types with genericsGo 1.22+
Type switchesConditional logic based on typeAll versions
reflect.Kind()Get the underlying kind of a typeAll versions

Here are some related Go tutorials you might find helpful:


Tested with Go 1.25+ | Last verified: December 2025

Happy coding! 🎉