Go > Core Go Basics > Control Flow > Switch statements
Go Switch Statement - Type Switch
This snippet demonstrates a type switch in Go. A type switch allows you to perform different actions based on the type of a variable. This is especially useful when working with interfaces.
Type Switch Structure
This code defines a function describe
that takes an interface{} as input. Inside the describe
function, a type switch
is used to determine the type of the interface value. The syntax v := i.(type)
is crucial. It declares a variable v
of the specific type that i
holds and assigns the value of i
to v
. The case
statements check for different types (int
, string
, bool
). The default
case handles any types not explicitly covered in the case
statements. In main
, the describe
function is called with different values to demonstrate the type switch.
package main
import "fmt"
func describe(i interface{}) {
switch v := i.(type) {
case int:
fmt.Printf("Integer: %d\n", v)
case string:
fmt.Printf("String: %s\n", v)
case bool:
fmt.Printf("Boolean: %t\n", v)
default:
fmt.Printf("Unknown type\n")
}
}
func main() {
describe(10)
describe("hello")
describe(true)
describe(10.5) // A float
}
Concepts Behind the Snippet
Type switches are a powerful feature in Go that allows you to dynamically determine the type of an interface value at runtime. This is particularly useful when working with functions that accept interface{} arguments, as it enables you to handle different types of input in a type-safe manner. The i.(type)
syntax is only valid inside a switch statement and is used to assert the type of the interface value.
Real-Life Use Case
Type switches are commonly used in scenarios where you need to process data from different sources or handle events of different types. For example, you might use a type switch to handle different types of messages received from a network connection or to process different types of events in a GUI application. Type switches are also valuable when implementing data serialization/deserialization logic when you need to dynamically unmarshal JSON or other data formats into Go structs based on the type information provided in the data.
Best Practices
default
case to handle unexpected or unknown types.
When to Use Them
Use type switches when you need to perform different actions based on the type of an interface value. If you only need to check for a single specific type, a type assertion with error checking might be more efficient. However, if you have multiple possible types to handle, a type switch is generally the most readable and maintainable solution.
Memory footprint
Type switches themselves don't add significant memory overhead. The memory footprint largely depends on the size and complexity of the data stored within the interface{} variable and the operations performed within each case. Type assertions involve a small runtime overhead for type checking.
alternatives
Alternatives to type switches are limited when dealing with interface{}. You could theoretically use a large if-else if chain with type assertions and error checking for each type, but this is much less readable and maintainable than a type switch.
pros
v := i.(type)
syntax ensures type safety within each case.
cons
FAQ
-
What happens if the interface value is nil?
If the interface value isnil
, the type switch will execute thedefault
case (if one is provided). If there is nodefault
case, the program will panic. -
Can I use a type switch with custom types?
Yes, you can use a type switch with custom types. Thecase
statements can check for any type, including structs, interfaces, and other user-defined types. Make sure your custom types are properly defined and imported into the package where you're using the type switch.