Type Assertions
Sometimes you have an interface value, but you need to access methods or fields that are only available on the underlying concrete type. You can do this using a type assertion.
Syntax
c, ok := s.(circle)
c: The value cast to the concrete type (circle).ok: A boolean that istrueif the assertion succeeded, orfalseif it failed.
Safe Assertion
Always use the “comma ok” idiom to avoid a panic if the interface doesn’t actually hold the type you’re asserting.
func printCircleRadius(s shape) {
if c, ok := s.(circle); ok {
fmt.Printf("Radius: %v\n", c.radius)
} else {
fmt.Println("Not a circle")
}
}