Type Assertion in Golang
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.
Transform Your Career
Choose from our industry-leading programs designed for career success
Modern Software and AI Engineering Program
Master full-stack development with AI integration
+1000 moreModern Data Science and ML with specialisation in AI
Advanced data science techniques with AI specialization
+1000 moreAdvanced AIML with Specialisation in Agentic AI
Deep dive into AIML with focus on Agentic systems
+1000 moreDevOps, Cloud & AI Platform Engineering
Build and manage AI-powered cloud infrastructure
+1000 moreAI Engineering Advanced Certification by IIT-Roorkee
Premier AI engineering certification from IIT-Roorkee
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.
Scaler Placement Report and Statistics
Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.
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.
Turn Learning into Career Growth
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.




