Type Assertion with Struct in Golang

Learn via video courses
Topics Covered

Overview

The type assertions in golang are used for accessing the type of an interface and also removing any sort of ambiguity from the variable of the interface. In this article, we will be discussing all types of assertions along with their use case.

Type Assertions

In the go language, the type assertions are used for extracting the type of the variable of the interface. If in the interface, the data type is present then the actual data type of the value will be retrieved that is present in the interface. The type assertion takes the value of the interface and extracts the explicit type value from it which is also known as removing any sort of ambiguity from the variable of the interface.

The syntax of the type assertion is as follows:

Where, The value is the variable whose type should be an interface The variable typeName is the type concrete that is used to check the value typeName is assigned to the variable t.

Type Assertion Example

The type assertions are used for revealing the concrete value of an interface variable. To understand this better, look at the example given below:

Example 1:

Output:

Explanation:

In this example, the variable I of the type interface is assigned a value of 42. The variable is then printed to the output which displays the value 42 at the output.

Example 2:

Golang program to illustrate the concept of type assertions

Output:

Explanation:

In this example, the interface value doesn’t hold the type int and the statement displays a panic error at the output which displays that the assertion of the type int failed. To check whether the value of the interface holds the specific type or not, the type assertion can return two values, the first is the variable named typeName and the second is the boolean value that is used for checking whether the assertion was successful or not. To understand this better, refer to example 3 given below.

Example 3:

Golang program to show the type assertions with error checking

Output:

Explanation:

In this example, we have created an interface that has an int value 20024 after which the value is retrieved of type int that is assigned to the value1 variable. Once the value is retrieved, the concrete value is printed. The test is used for testing the interface that has a type of string and returns true if the value is found else it returns false.

Determining Types

The type that can take any value is called an interface. It creates ambiguity since nothing can be said about that variable. The type assertions and type switches can also help us understand the kind of variable that it contains along with its type.

Example:

Output:

Explanation:

In this example, the type assertion is used by using the syntax value, ok := name.(string) which asserts the type which has a variable name. If the variable is of type string, then the value is corrected and is then assigned to the value after which it is assigned true. When the negative case is taken into consideration, that asserts that Elliot is assigned to the variable value and ok is assigned to true. In this case, the intValue is then assigned to 0 and ok is assigned as false.

Struct Assertion Example - Error Matching

The type assertions are mainly used for removing ambiguity from the interface variable along with unwrapping the concrete values inside the interface variable.

One of the most common use cases for type assertion is to assert the type of value that is being returned so that we can rectify the error and code logic.

To understand this better, let’s take an example. Let us assume that there is a function that returns two types of errors which are ErrBadInput and ErrCouldNotProcess. While handling the code that arises from this function, we might want the error to be handled differently depending on the type of error. But to determine what type of error is thrown, the assertion is used.

Example:

Output:

Explanation:

In this example, we have used the type switcher for switching between different cases depending on the type of error that is thrown from the function SendsAnError. It checks the switch cases and then returns the specific type of error at the output which in this case is a bad input error.

Conclusion

  • The type assertions in golang are used for accessing the type of an interface and also removing any sort of ambiguity from the variable of the interface.
  • In the go language, the type assertions are used for extracting the type of the variable of the interface. If in the interface, the data type is present then the actual data type of the value will be retrieved that is present in the interface.
  • The type assertions are mainly used for removing ambiguity from the interface variable along with unwrapping the concrete values inside the interface variable. One of the most common use cases for type assertion is to assert the type of value that is being returned so that we can rectify the error and code logic.
  • The type that can take any value is called an interface. It creates ambiguity since nothing can be said about that variable. The type assertions and type switches can also help us understand the kind of variable that it contains along with its type.
  • The syntax of the type assertion is as follows:
  • The value is the variable whose type should be an interface
  • The variable typeName is the type concrete that is used to check the value typeName is assigned to the variable t.