Type Assertions and Type Switches in Golang

A type assertion in Golang is “used to provide access to an interface value’s underlying concrete value”

A type assertion does not convert an interface to another data type. Instead, it provides access to the underlying concrete value of the interface. This is normally what you want, as it allows you to work with the interface’s value as if it were of the concrete type.

Syntax

t := variable.(T)

Parameters

  1. variable: It is a variable to be asserted.
  2. T: It is the type that is expected.

This statement asserts that the interface value variable holds the concrete type T and assigns the underlying T value to the variable t.

Return value

The type assertion returns a value of the specified type or nil if the variable is not of that type.

Example 1: How to use type assertion

Here is an example of how to use a type assertion:

package main

import "fmt"

func main() {
  var x interface{} = "Krunal, Lathiya!"

  // Assert that x is a string.
  s, ok := x.(string)

  if ok {
    fmt.Println("The value of x is", s)
  } else {
    fmt.Println("The value of x is not a string")
  }
}

Output

The value of x is Krunal, Lathiya!

Example 2: panic: interface conversion: interface {} is string, not float64

The error message panic: interface conversion: interface {} is string, not float64 occurs when you attempt a type assertion where you assumed an interface{} value was of type float64, but in reality, it was of type string.

package main

import "fmt"

func main() {
  var x interface{} = "Krunal, Lathiya!"

  f, ok := x.(float64)
  fmt.Println(f, ok)

  f = x.(float64)
  fmt.Println(f)
}

Output

panic: interface conversion: interface {} is string, not float64

To handle this panic, you can use the “num, ok” idiom for type assertions, which allows you to safely check whether the interface value is of a certain type:

package main

import "fmt"

func main() {
  var x interface{} = "Krunal, Lathiya!"

  num, ok := x.(float64)
  if ok {
    fmt.Println("Conversion successful, x is a float64:", num)
  } else {
    fmt.Println("Conversion failed, x is not a float64")
  }
}

Output

Conversion failed, x is not a float64

Type Switches in Golang

A type switch in Golang is “used to perform several type assertions in series and runs the first case with a matching type”. This is helpful when you need to check the variable type and execute different codes depending on the variable type.

Syntax

switch variable {
case type1:
  // Code to be executed if the variable is of type1.
case type2:
  // Code to be executed if the variable is of type2.
default:
  // Code to be executed if the variable is of no known type.
}

Where:

  1. variable: It is a variable to be switched on.
  2. type1, type2, etc: They are the types that are expected.

Example

package main

import "fmt"

func main() {
  var data interface{} = 21.19

  switch data.(type) {
    case string:
      fmt.Println("The value of data is a string")
    case int:
      fmt.Println("The value of data is an integer")
    case float64:
      fmt.Println("The value of data is an float64")
    default:
      fmt.Println("The value of data is of an unknown type")
   }
 }

Output

The value of data is an float64

The type switch can correctly identify the variable type and print the appropriate message.

That’s it!