Type Assertions in Golang Last Updated : 22 Jun, 2020 Summarize Comments Improve Suggest changes Share Like Article Like Report Type assertions in Golang provide access to the exact type of variable of an interface. If already the data type is present in the interface, then it will retrieve the actual data type value held by the interface. A type assertion takes an interface value and extracts from it a value of the specified explicit type. Basically, it is used to remove the ambiguity from the interface variables. Syntax: t := value.(typeName) where value is a variable whose type must be an interface, typeName is the concrete type we want to check and underlying typeName value is assigned to variable t. Example 1: C // Golang program to illustrate // the concept of type assertions package main import ( "fmt" ) // main function func main() { // an interface that has // a string value var value interface{} = "GeeksforGeeks" // retrieving a value // of type string and assigning // it to value1 variable var value1 string = value.(string) // printing the concrete value fmt.Println(value1) // this will panic as interface // does not have int type var value2 int = value.(int) fmt.Println(value2) } Output: GeeksforGeeks panic: interface conversion: interface {} is string, not int In the above code, since the value interface does not hold an int type, the statement triggered panic and the type assertion fails. To check whether an interface value holds a specific type, it is possible for type assertion to return two values, the variable with typeName value and a boolean value that reports whether the assertion was successful or not. This is shown in the following example: Example 2: C // Golang program to show type // assertions with error checking package main import ( "fmt" ) // main function func main() { // an interface that has // an int value var value interface{} = 20024 // retrieving a value // of type int and assigning // it to value1 variable var value1 int = value.(int) // printing the concrete value fmt.Println(value1) // this will test if interface // has string type and // return true if found or // false otherwise value2, test := value.(string) if test { fmt.Println("String Value found!") fmt.Println(value2) } else { fmt.Println("String value not found!") } } Output: 20024 String value not found! Comment More infoAdvertise with us Next Article Type Assertions in Golang V vanigupta20024 Follow Improve Article Tags : Programming Language Go Language Golang-Misc Similar Reads Generics in Golang Generics are essentially template/boilerplate code written in a form to use on the Go with types that can be added later. The main aim of generics is to achieve greater flexibility in terms of writing code with the addition of fewer lines. The concept of Generics has existed for a long time now and 4 min read Composition in Golang Composition is a method employed to write re-usable segments of code. It is achieved when objects are made up of other smaller objects with particular behaviors, in other words, Larger objects with a wider functionality are embedded with smaller objects with specific behaviors. The end goal of compo 5 min read Go Type Assertion vs Type Switches Type Assertion allows you to access the underlying concrete type of an interface. When you have a variable of type interface{}, you might want to access its actual type (the type it holds), and Type Assertion helps with that.Differences Between Type Assertion and Type SwitchFeatureType AssertionType 5 min read Data Types in Go Data types specify the type of data that a valid Go variable can hold. In Go language, the type is divided into four categories which are as follows: Basic type: Numbers, strings, and booleans come under this category.Aggregate type: Array and structs come under this category.Reference type: Pointer 7 min read Function as a Field in Golang Structure In Go, you can define functions as fields within a structure (struct). This feature allows you to associate behaviors (methods) directly with data types, enabling a more organized and encapsulated way of managing data and its related operations.Examplepackage mainimport "fmt"// Define a struct with 2 min read Like