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.

Transform Your Career

Choose from our industry-leading programs designed for career success

NSDC Certified

Modern Software and AI Engineering Program

Master full-stack development with AI integration

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Modern Data Science and ML with specialisation in AI

Advanced data science techniques with AI specialization

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Advanced AIML with Specialisation in Agentic AI

Deep dive into AIML with focus on Agentic systems

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

DevOps, Cloud & AI Platform Engineering

Build and manage AI-powered cloud infrastructure

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

AI Engineering Advanced Certification by IIT-Roorkee

Premier AI engineering certification from IIT-Roorkee

3 MonthsDuration
AI-LedCurriculum
Career SupportSupport
Program highlights
Go to Program

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.

Free Courses by top Scaler instructors
Python Course for Beginners With Certification: Mastering the Essentials
Java Course - Mastering the Fundamentals
DBMS Course - Master the Fundamentals and Advanced Concepts
JavaScript Course With Certification: Unlocking the Power of JavaScript
C++ Course: Learn the Essentials
Python and SQL for Data Science
Python Course for Beginners With Certification: Mastering the Essentials
Java Course - Mastering the Fundamentals
DBMS Course - Master the Fundamentals and Advanced Concepts
JavaScript Course With Certification: Unlocking the Power of JavaScript
C++ Course: Learn the Essentials
Python and SQL for Data Science

Program to Illustrate the Concept of Type Assertion

The following program demonstrates the concept of type assertion.

Scaler Placement Report and Statistics

₹23L
AVG CTC
SCALER PLACEMENT PROOF

Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.

11,000+placements
650+companies
Verified data
Hiring Partners:
GoogleGoogleAmazonAmazonMicrosoftMicrosoftFlipkartFlipkartAdobeAdobe1200+ more

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

1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary
1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary

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.