xxxxxxxxxx
iAreaId := val.(int)
iAreaId, ok := val.(int) // Alt. non panicking version
xxxxxxxxxx
var myInt interface{}
myInt = 8
toInt, ok := myInt.(int)
fmt.Println(toInt, ok) // 8 true
toString, ok := myInt.(string)
fmt.Println(toString, ok) // "" false
xxxxxxxxxx
package main
import (
"fmt"
)
type animal interface {
description() string
}
type cat struct {
Type string
Sound string
}
type snake struct {
Type string
Poisonous bool
}
func (s snake) description() string {
return fmt.Sprintf("Poisonous: %v", s.Poisonous)
}
func (c cat) description() string {
return fmt.Sprintf("Sound: %v", c.Sound)
}
func main() {
var a animal
a = snake{Poisonous: true}
fmt.Println(a.description())
a = cat{Sound: "Meow!!!"}
fmt.Println(a.description())
}
//=> Poisonous: true
//=> Sound: Meow!!!