Go Type Assertion vs Type Switches
Last Updated :
04 Feb, 2025
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 Switch
Feature | Type Assertion | Type Switch |
---|
Purpose | Extracts the concrete type of an interface. | Compares an interface against multiple types. |
Use Case | When you know the concrete type. | When dealing with multiple potential types. |
Handling Failures | Can panic if the assertion fails. | Gracefully handles unmatched types with a default case. |
Return Type | Returns the concrete value and a success flag (ok ). | Directly accesses the value in each case. |
Type Assertion
When you assert the type of an interface variable, Go will attempt to convert the interface to the specified concrete type. If successful, you can work with the concrete type directly. If not, Go will panic unless you handle it properly using the comma-ok idiom.
Basic Syntax
i.(T) Here, i
is an interface, and T
is the type you want to assert.
Basic Usage
package main
import "fmt"
func main() {
var i interface{} = "Hello" // 'i' holds a string
s := i.(string) // Assert that i is of type string
fmt.Println(s) // Output: Hello
}
In this example, we assert that i
holds a string
, and the program successfully prints "Hello".
Asserting Primitive Types
package main
import "fmt"
func main() {
var i interface{} = 55 // 'i' holds an int
if v, ok := i.(int); ok {
fmt.Println("integer:", v) // Output: integer: 55
}
}
Here, i
is asserted as an int
. If the assertion is correct, v
gets the value of i
and prints it. The ok
variable is used to check if the assertion was successful. If the assertion fails, ok
will be false
, and no action is performed.
Asserting Composite Types
package main
import "fmt"
func main() {
var i interface{} = []string{"apple", "banana", "cherry"} // 'i' holds a slice of strings
if v, ok := i.([]string); ok {
fmt.Println("slice of strings:", v) // Output: slice of strings: [apple banana cherry]
}
}
Here, i
is asserted as a slice of strings. If the assertion succeeds, the slice is printed.
Asserting Interface Types
You can also use Type Assertion to check if an interface satisfies another interface. This is especially useful when dealing with multiple interface types or method sets.
Go
package main
import "fmt"
type reader interface { read() }
type writer interface { write() }
type readWriter interface { reader; writer }
type document struct{}
func (d *document) read() { fmt.Println("Reading document") }
func (d *document) write() { fmt.Println("Writing document") }
func main() {
var rw readWriter = &document{}
if r, ok := rw.(reader); ok {
fmt.Println("rw satisfies reader:", r) // Output: rw satisfies reader: &{}
}
}
The reader
and writer
interfaces define read()
and write()
methods, respectively, while readWriter
combines both, and the document
type implements them; the main
function checks if rw
satisfies the reader
interface and prints the result.
Handling Failures
If you assert a type that the interface does not hold, Go will panic unless you use the comma-ok idiom to safely handle the failure.
package main
import "fmt"
func main() {
var i interface{} = "Hello"
f := i.(float64) // This triggers a panic because 'i' holds a string, not a float64
fmt.Println(f)
}
In this case, the program panics because we're trying to assert a float64
from an interface that holds a string. To prevent this, use the comma-ok idiom.
Type Switches
A Type Switch is similar to a regular switch
statement but is specifically used for comparing an interface variable’s type against multiple types. It allows you to evaluate the type of an interface variable and handle it accordingly.
Basic Usage Example
package main
import "fmt"
func main() {
var i interface{} = 7
switch i.(type) {
case int:
fmt.Println("i is an int") // Output: i is an int
case string:
fmt.Println("i is a string")
default:
fmt.Println("unknown type")
}
}
In this example, i
holds an integer, and the type switch evaluates its type and prints the appropriate message.
Using a Variable in a Type Switch Case
You can also assign the value of i
to a variable inside the case, allowing you to work with the extracted value.
package main
import "fmt"
func main() {
var i interface{} = []byte("hello")
switch v := i.(type) {
case []byte:
fmt.Println(string(v)) // Output: hello
case string:
fmt.Println(v)
}
}
Here, v
holds the actual value of the asserted type ([]byte
), and we can use it directly within the case.
Handling Multiple Types
A type switch can handle multiple types using the ,
syntax, similar to an OR relationship.
package main
import "fmt"
func main() {
var i interface{} = 2.5
switch i.(type) {
case int, float64:
fmt.Println("i is a number") // Output: i is a number
case string:
fmt.Println("i is a string")
}
}
In this example, i
can either be an int
or float64
, and the case evaluates both types, printing that i
is a number.
Addressing Composite Types
Type switches can also be used to evaluate composite types like slices, maps, and structs.
package main
import "fmt"
func main() {
var i interface{} = map[string]bool{"hello": true, "world": false}
switch i.(type) {
case map[string]bool:
fmt.Println("i is a map") // Output: i is a map
case []string:
fmt.Println("i is a slice")
default:
fmt.Println("unknown type")
}
}
Here, we check whether i
is a map[string]bool
or []string
. Since i
is a map, it prints i is a map
.
Conclusion
Type Assertions and Type Switches are key tools for working with interfaces in Go:
- Type Assertions are used when you're certain about the type of the interface and want to extract its concrete value.
- Type Switches are more flexible, allowing you to handle multiple types in a clean, readable way.
Using these constructs properly helps you write efficient, maintainable Go code, especially in applications with dynamic or complex type handling.
Similar Reads
Type Assertions in Golang 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 specifie
2 min read
Type Switches in GoLang A switch is a multi-way branch statement used in place of multiple if-else statements but can also be used to find out the dynamic type of an interface variable. A type switch is a construct that performs multiple type assertions to determine the type of variable (rather than values) and runs the fi
3 min read
Different Ways to Find the Type of Variable in Golang Variable is a placeholder of the information which can be changed at runtime. And variables allow to Retrieve and Manipulate the stored information. There are 3 ways to find the type of variables in Golang as follows: Using reflect.TypeOf Function Using reflect.ValueOf.Kind() Function Using %T with
2 min read
Different Ways to Find the Type of Variable in Golang Variable is a placeholder of the information which can be changed at runtime. And variables allow to Retrieve and Manipulate the stored information. There are 3 ways to find the type of variables in Golang as follows: Using reflect.TypeOf Function Using reflect.ValueOf.Kind() Function Using %T with
2 min read
Different Ways to Find the Type of an Object in Golang Go does not have the concept of a class type, therefore, you do not have objects in Go. You can refer any data type in Go as an object. There are several data types provided by Go such as int8, int16, int32, int64, float64, string, bool etc. There are three different ways by which you can find the t
4 min read
Different Ways to Find the Type of an Object in Golang Go does not have the concept of a class type, therefore, you do not have objects in Go. You can refer any data type in Go as an object. There are several data types provided by Go such as int8, int16, int32, int64, float64, string, bool etc. There are three different ways by which you can find the t
4 min read