Skip to content

Instantly share code, notes, and snippets.

@mkock
Created October 2, 2021 20:19
Show Gist options
  • Save mkock/558ec077627c50201e6bea21bfac10fd to your computer and use it in GitHub Desktop.
Save mkock/558ec077627c50201e6bea21bfac10fd to your computer and use it in GitHub Desktop.
type movie string
func (m movie) Movie() string { return string(m) }
type movieStar string
func (m movieStar) Star() string { return string(m) }
func movieOrStar(m interface{}) string {
switch v := m.(type) {
case interface{ Star() string }:
return v.Star()
case interface{ Movie() string }:
return v.Movie()
default:
return ""
}
}
func main() {
var value interface{}
value = movieStar("Sylvester Stallone")
fmt.Println(movieOrStar(value)) // Prints "Sylvester Stallone
value = movie("Rambo")
fmt.Println(movieOrStar(value)) // Prints "Rambo"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment