
- Go - Home
- Go - Overview
- Go - Environment Setup
- Go - Program Structure
- Go - Basic Syntax
- Go - Data Types
- Go - Variables
- Go - Constants
- Go - Identifiers
- Go - Keywords
- Go - Operators
- Go - Arithmetic Operators
- Go - Assignment Operators
- Go - Relational Operators
- Go - Logical Operators
- Go - Bitwise Operators
- Go - Miscellaneous Operators
- Go - Operators Precedence
- Go Decision Making
- Go - Decision Making
- Go - If Statement
- Go - If Else Statement
- Go - Nested If Statements
- Go - Switch Statement
- Go - Select Statement
- Go Control Flow Statements
- Go - For Loop
- Go - Nested for Loops
- Go - Break Statement
- Go - Continue Statement
- Go - Goto Statement
- Go Functions
- Go - Functions
- Go - Call by Value
- Go - Call by Reference
- Go - Functions as Values
- Go - Function Closure
- Go - Function Method
- Go - Anonymous function
- Go Strings
- Go - Strings
- Go - String Length
- Go - String Concatenation
- Go - Compare Strings
- Go - Split String
- Go - Substring Extraction
- Go - String Replacement
- Go - String Interpolation
- Go - Parse Date Strings
- Go Arrays
- Go - Arrays
- Go - Multidimensional Arrays
- Go - Multidimensional Arrays
- Go - Passing Arrays to Functions
- Go - Pointers
- Go - Pointers
- Go - Array of pointers
- Go - Pointer to pointer
- Go - Passing pointers to functions
- Go Advanced Control Structures
- Go - Scope Rules
- Go - Dereferencing Pointer
- Go - Structures
- Go - Slice
- Go - Slice of Slices
- Go - Range
- Go - Maps
- Go - Recursion
- Go - Type Casting
- Go - Interfaces
- Go - Type Assertion
- Go - Error Handling
- Go - Concurrency
- Go - Regular Expression
- Go - Inheritance
- Go - Packages
- Go - Templates
- Go - Reflection
- Go - Generics
- Go File Handling
- Go - Read File By Word
- Go - Read File By Line
- Go - Read CSV Files
- Go - Delete File
- Go - Rename & Move File
- Go - Truncate a File
- Go - File Read-Write Mode W/O Truncation
- Go Miscellaneous
- Go - defer Keyword
- Go - Fmt Package
- Go - Zero Value
- Go - Import
Go - Type Assertion
The type assertion in Golang is a way to retrieve the dynamic value of an interface. It is used to extract the underlying value of an interface and convert it to a specific type. This is useful when you have an interface with a value of an unknown type and you need to operate on that value.
Following is the syntax to the type assertion in Golang −
t := Value.(TypeName)
Basic Type Assertion
The basic type assertion is a way to inform the compiler about the specific type of a variable when it can't automatically determine it that helps to avoid runtime errors.
Example
In this example, we assert the type of an interface value to a string.
package main import ( "fmt" ) func main() { var i interface{} = "hello" // Type assertion to retrieve the string value s := i.(string) fmt.Println(s) }
When the above code is compiled and executed, it produces the following result −
hello
Type Assertion with Ok-idiom
The Ok-idiom is a common pattern for handling errors, and it's often used with type assertions to ensure that operations succeed.
Example
In this example, we use the Ok-idiom to check if the type assertion is successful or not to avoid panics.
package main import ( "fmt" ) func main() { var i interface{} = "hello" // Type assertion with Ok-idiom if s, ok := i.(string); ok { fmt.Println("String value:", s) } else { fmt.Println("Not a string") } }
When the above code is compiled and executed, it produces the following result −
String value: hello
Handling Failed Type Assertion
Handling a failed type assertion involves managing the scenario where a type assertion does not hold, and the program needs to handle the resulting error.
Example
In this example, we will ensure that what happens when the type assertion fails.
package main import ( "fmt" ) func main() { var i interface{} = 42 // Attempting to assert to a wrong type if s, ok := i.(string); ok { fmt.Println(s) } else { fmt.Println("Type assertion failed") } }
When the above code is compiled and executed, it produces the following result −
Type assertion failed