Skip to content

Instantly share code, notes, and snippets.

@inhzus
Last active August 6, 2019 11:52
Show Gist options
  • Save inhzus/b301db257c520c15466fc833aaaec7f6 to your computer and use it in GitHub Desktop.
Save inhzus/b301db257c520c15466fc833aaaec7f6 to your computer and use it in GitHub Desktop.
import "fmt"
type Other struct {
c bool
x, y int
}
type Model interface{}
type A struct {
x int
}
type B struct {
A
y int
}
func NewModel(other *Other) Model {
if other.c {
return &A{x: other.x,}
} else {
return &B{
A: A{x: other.x,},
y: other.y,
}
}
}
func test(model Model) {
if a, ok := model.(*A); ok {
fmt.Printf("x: %v", a.x)
} else {
b := model.(*B)
fmt.Printf("x: %v, y: %v", b.x, b.y)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment