Skip to content

Instantly share code, notes, and snippets.

@mkock
Last active October 3, 2021 09:24
Show Gist options
  • Select an option

  • Save mkock/5050aba5d1213b2b320c2b3cfebd7a89 to your computer and use it in GitHub Desktop.

Select an option

Save mkock/5050aba5d1213b2b320c2b3cfebd7a89 to your computer and use it in GitHub Desktop.
type player struct {
// unexported fields
}
func (p *player) status() string {
return "Player is eating"
}
func (p *player) sleep() {
// implementation goes here
}
type monster struct {
// unexported fields
}
func (m *monster) status() string {
return "Monster is sleeping"
}
func (m *monster) sleep() {
// implementation goes here
}
type character interface {
status() string
sleep()
}
func getStatus(c character) string {
return c.status()
}
func main() {
cyclops := new(monster)
playerOne := new(player)
fmt.Println(getStatus(cyclops)) // Prints "Monster is sleeping"
fmt.Println(getStatus(playerOne)) // Prints "Player is eating"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment