0
0
GoHow-ToBeginner · 3 min read

How to Check Type of Interface Value in Go: Simple Guide

In Go, you can check the type of a value stored in an interface using a type assertion or a type switch. A type assertion tries to convert the interface to a specific type, while a type switch lets you handle multiple types safely.
📐

Syntax

To check the type of an interface value, you use either a type assertion or a type switch.

  • value, ok := interfaceValue.(Type): This tries to convert interfaceValue to Type. If successful, ok is true.
  • switch v := interfaceValue.(type) { ... }: This checks the actual type of interfaceValue and lets you handle each type case.
go
var i interface{} = "hello"

// Type assertion
s, ok := i.(string)
if ok {
    // s is string
}

// Type switch
switch v := i.(type) {
case string:
    // v is string
case int:
    // v is int
default:
    // unknown type
}
💻

Example

This example shows how to check the type of an interface value using both type assertion and type switch. It prints the type and value if recognized.

go
package main

import "fmt"

func main() {
    var i interface{}

    i = 42

    // Using type assertion
    if v, ok := i.(int); ok {
        fmt.Printf("Type assertion: value is int: %d\n", v)
    } else {
        fmt.Println("Type assertion: not an int")
    }

    // Using type switch
    switch v := i.(type) {
    case int:
        fmt.Printf("Type switch: int with value %d\n", v)
    case string:
        fmt.Printf("Type switch: string with value %s\n", v)
    default:
        fmt.Println("Type switch: unknown type")
    }
}
Output
Type assertion: value is int: 42 Type switch: int with value 42
⚠️

Common Pitfalls

Common mistakes include:

  • Using a type assertion without checking the ok value, which can cause a panic if the type does not match.
  • Assuming the interface holds a specific type without verifying it first.
  • Not handling the default case in a type switch, which can lead to missed cases.
go
package main

import "fmt"

func main() {
    var i interface{} = "hello"

    // Wrong: no check, will panic if type mismatches
    // s := i.(int) // panic: interface conversion: string is not int

    // Right: check with ok
    s, ok := i.(int)
    if !ok {
        fmt.Println("Type assertion failed: not an int")
    } else {
        fmt.Println(s)
    }
}
Output
Type assertion failed: not an int
📊

Quick Reference

ConceptSyntaxDescription
Type Assertionvalue, ok := i.(Type)Try to convert interface to Type, ok is true if successful
Type Switchswitch v := i.(type) { case Type1: ... }Check interface type and handle multiple types
Check okif ok { ... } else { ... }Avoid panic by verifying type assertion success
Default casedefault: ...Catch all other types in type switch

Key Takeaways

Use type assertions with the ok variable to safely check interface types.
Type switches let you handle multiple possible types cleanly.
Always handle the default case in type switches to avoid missing types.
Avoid panics by never assuming the interface holds a specific type without checking.
Type checking helps write safer and clearer Go code when working with interfaces.