Sling Academy
Home/Golang/Type Assertions and Type Switches with Interfaces in Go

Type Assertions and Type Switches with Interfaces in Go

Last updated: November 26, 2024

Introduction

When working with interfaces in Go, it’s common to encounter situations where you need to determine the underlying concrete type or handle multiple types. Go provides two powerful constructs to achieve this: type assertions and type switches.

Type Assertions

Type assertions provide a way to retrieve the dynamic type of an interface. It checks whether the interface value holds a specific type, and if so, extracts and returns that type.

Basic Type Assertion


package main

import (
    "fmt"
)

func main() {
    var i interface{} = "Hello, GoLang!"

    // Attempting a type assertion to a string
    str, ok := i.(string)
    if ok {
        fmt.Println("The string is:", str)
    } else {
        fmt.Println("i is not a string")
    }
}

In this example, i.(string) checks if i holds a string type. If it does, the assertion succeeds and assigns the result to str.

Advanced Type Assertion


package main

import (
    "fmt"
)

func main() {
    var i interface{} = 42

    f, ok := i.(float64)
    if !ok {
        fmt.Println("i is not a float64, it's:", i)
    } else {
        fmt.Printf("i is a float64 and its value is %f\n", f)
    }
}

Here, the assertion fails since i is not float64, demonstrating how ok can be used to gracefully handle type assertion failures.

Type Switches

Type switches are a more expressive way to compare the types of an interface value against multiple types. It’s like a switch statement but specifically designed to work with types.

Basic Type Switch


package main

import (
    "fmt"
)

func main() {
    checkType := func(i interface{}) {
        switch v := i.(type) {
        case int:
            fmt.Printf("The integer value is %d\n", v)
        case string:
            fmt.Printf("The string value is '%s'\n", v)
        default:
            fmt.Printf("Unknown type %T\n", v)
        }
    }

    checkType(10)
    checkType("Hello, world!")
    checkType(34.6)
}

The code checks the type of i and performs operations accordingly.

Complex Type Switch


package main

import (
    "fmt"
)

func printDetails(i interface{}) {
    switch v := i.(type) {
    case nil:
        fmt.Println("Value is nil")
    case int, int32, int64:
        fmt.Printf("The integer with value: %v\n", v)
    case float32, float64:
        fmt.Println("This is a float: ", v)
    case bool:
        fmt.Println("Boolean value: ", v)
    default:
        fmt.Printf("Not sure what type the value is — %T\n", v)
    }
}

func main() {
    printDetails(nil)
    printDetails(100)
    printDetails(12.345)
    printDetails(true)
    printDetails("an unhandled type")
}

The switch considers more types and handles a few of them collectively, like integer and floating-point categories.

Conclusion

Type assertions and type switches in Go are robust tools that provide flexibility when working with interfaces. They allow you to safely work with values of different types, leading to increased code safety and expressiveness. Understanding these concepts is crucial for advanced Go programming.

Next Article: Combining Interfaces for Modular and Flexible Design in Go

Previous Article: Defining and Using Empty Interfaces (`interface{}`) in Go

Series: Structs and Interfaces in Go

Golang

Related Articles

You May Also Like

  • How to remove HTML tags in a string in Go
  • How to remove special characters in a string in Go
  • How to remove consecutive whitespace in a string in Go
  • How to count words and characters in a string in Go
  • Relative imports in Go: Tutorial & Examples
  • How to run Python code with Go
  • How to generate slug from title in Go
  • How to create an XML sitemap in Go
  • How to redirect in Go (301, 302, etc)
  • Using Go with MongoDB: CRUD example
  • Auto deploy Go apps with CI/ CD and GitHub Actions
  • Fixing Go error: method redeclared with different receiver type
  • Fixing Go error: copy argument must have slice type
  • Fixing Go error: attempted to use nil slice
  • Fixing Go error: assignment to constant variable
  • Fixing Go error: cannot compare X (type Y) with Z (type W)
  • Fixing Go error: method has pointer receiver, not called with pointer
  • Fixing Go error: assignment mismatch: X variables but Y values
  • Fixing Go error: array index must be non-negative integer constant