Goのinterfaceのunionsの例
package main
import "fmt"
type Foo struct {
foo string
}
type Bar struct {
bar string
}
type Baz interface {
Foo | Bar
}
func f[T Baz](a T) {
switch b := any(a).(type) {
case Foo:
fmt.Println(b.foo)
case Bar:
fmt.Println(b.bar)
}
}
func main() {
a := Foo{foo: "foo"}
b := Bar{bar: "bar"}
f(a)
f(b)
}