Search⌘ K

The Type Switch

Explore how to use type switches in Go for testing the types of interface variables. This lesson helps you understand runtime type analysis, handling multiple types within interfaces, and applying type switches to process diverse data effectively in your Go programs.

We'll cover the following...

Testing the type of interface #

The type of an interface variable can also be tested with a special kind of switch: the type-switch. Look at the following program:

Go (1.6.2)
package main
import (
"fmt"
"math"
)
type Square struct {
side float32
}
type Circle struct {
radius float32
}
type Shaper interface {
Area() float32
}
func main() {
var areaIntf Shaper
sq1 := new(Square)
sq1.side = 5
areaIntf = sq1
switch t := areaIntf.(type) {
case *Square:
fmt.Printf("Type Square %T with value %v\n", t, t)
case *Circle:
fmt.Printf("Type Circle %T with value %v\n", t, t)
default:
fmt.Printf("Unexpected type %T", t)
}
}
func (sq *Square) Area() float32 {
return sq.side * sq.side
}
func (ci *Circle) Area() float32 {
return ci.radius * ci.radius * math.Pi
}

The variable t receives both value and type from areaIntf. All of ...