Last active
August 6, 2019 11:52
-
-
Save inhzus/b301db257c520c15466fc833aaaec7f6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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