100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
Go

Type Assertions and Type Switches in Go

Learn how to safely extract and branch on the concrete type stored inside a Go interface value.

Interfaces & CompositionIntermediate8 min readJul 8, 2026
Analogies

Introduction

An interface value in Go can hold any concrete type that satisfies it, but sometimes code needs to recover that specific underlying type to use type-specific behavior. Type assertions let you extract the concrete value stored in an interface, and type switches let you branch over several possible concrete types in one construct. Both are essential tools when working with the empty interface (interface{} / any) or with interfaces implemented by multiple different types.

🏏

Cricket analogy: A team sheet lists 'all-rounder' but during the match you need to know if that player is specifically a leg-spinner like Yuzvendra Chahal to decide the bowling order, so you check their specific skill beyond the generic label.

Syntax

go
// Type assertion (single value form, panics on failure)
s := i.(string)

// Type assertion (comma-ok form, safe)
v, ok := i.(SomeType)

// Type switch
switch v := i.(type) {
case int:
    fmt.Println("int:", v)
case string:
    fmt.Println("string:", v)
default:
    fmt.Println("unknown type")
}

Explanation

The single-value assertion i.(string) attempts to extract a string from interface value i; if i does not actually hold a string, the program panics. The safer comma-ok form v, ok := i.(SomeType) instead returns a zero value and ok == false on failure, letting the program handle the mismatch gracefully instead of crashing. A type switch is a cleaner alternative when checking against several possible types: the special syntax i.(type) is only valid inside a switch statement, and each case compares against a concrete type (or another interface type), binding v to the correctly typed value within that case's block.

🏏

Cricket analogy: Confidently declaring 'this batsman is Virat Kohli' without checking scorecards is like i.(string) - if wrong, the commentary team panics on air, whereas checking the scorecard first, ok := i.(Kohli), lets you gracefully say 'not confirmed' instead.

Example

go
package main

import "fmt"

func describe(i interface{}) {
    switch v := i.(type) {
    case int:
        fmt.Printf("integer: %d\n", v)
    case string:
        fmt.Printf("string of length %d: %q\n", len(v), v)
    case bool:
        fmt.Printf("boolean: %t\n", v)
    default:
        fmt.Printf("unsupported type: %T\n", v)
    }
}

func main() {
    describe(42)
    describe("hello")
    describe(3.14)
}

Output

go
integer: 42
string of length 5: "hello"
unsupported type: float64

Key Takeaways

  • v, ok := i.(T) safely checks and extracts the concrete type without panicking.
  • i.(T) alone panics at runtime if i does not hold a value of type T.
  • Type switches (switch v := i.(type) { ... }) branch cleanly over multiple possible types.
  • The %T verb in fmt is useful for printing the dynamic type of an interface value.

Practice what you learned

Was this page helpful?

Topics covered

#Go#GoProgrammingStudyNotes#Programming#TypeAssertionsAndTypeSwitchesInGo#Type#Assertions#Switches#Syntax#Testing#StudyNotes#SkillVeris