Type Assertion in Golang

Learn via video courses
Topics Covered

Overview

Type assertions, as the name implies, are used to assert that a variable is of a specific type. Only interfaces are allowed to use type assertions. A type assertion does not convert the data type of variable of an interface to another data type, but it does provide access to the exact type of variable of an interface. If the data type is already present in the interface, it will retrieve the actual data type value stored in the interface.

What are the Roles of Type Assertions in Golang?

In Golang, type assertions provide access to the exact type of variable of an interface. Type assertions can only occur on interfaces. It does not convert an interface to another data type. If the data type is already present in the interface, it will retrieve the actual data type value stored in the interface. A type assertion extracts a value of the specified, explicit type from an interface value. It is primarily used to remove ambiguity from interface variables.

Syntax of Type Assertion

The syntax of the type assertion is illustrated below.

In the above syntax, val is a variable whose type has to be an interface. Type_name is the concrete type that interface value val holds, and assigns the underlying Type_name value to the variable t.

Program to Illustrate the Concept of Type Assertion

The following program demonstrates the concept of type assertion.

Implementation:

Output:

Explanation:

In the above program, we assigned the string value Scaler to the interface denoted by val. Following that, we retrieve a string value, assign it to the val1 variable, and then print it. When we try to retrieve an int value in the following step, the statement panics and the type assertion fails because the value interface does not hold an int type.

Program to Show Type Assertion with Error Checking

The following program shows type assertions with error checking.

Implementation

Output:

Explanation:

In the above Program, we assigned the string value Scaler to the interface denoted by val. We use the first type assertion to get the underlying value of val as a string, assign it to the variable val1, and then print it. We use the second type assertion to get the underlying value of val as an int and assign it to the variable val2. Here we also get the boolean value ok, which prints the value of val2 if the type assertion was successful. If the type assertion fails, we print a message indicating that the type assertion failed.

Conclusion

  • In Golang, type assertions provide access to the exact type of variable of an interface.
  • Type assertions can only occur on interfaces.
  • A type assertion does not convert the data type of variable of an interface to another data type, but it does provide access to the exact type of variable of an interface.
  • A type assertion extracts a value of the specified, explicit type from an interface value.
  • A type assertion is primarily used to remove ambiguity from interface variables.