Created
October 2, 2021 20:19
-
-
Save mkock/558ec077627c50201e6bea21bfac10fd 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
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