Skip to content

Instantly share code, notes, and snippets.

@arriqaaq
Last active January 26, 2023 07:17
Show Gist options
  • Select an option

  • Save arriqaaq/de8c2f9fd93a8a9c4e364a6e354f61f7 to your computer and use it in GitHub Desktop.

Select an option

Save arriqaaq/de8c2f9fd93a8a9c4e364a6e354f61f7 to your computer and use it in GitHub Desktop.
type Car interface {
Drive() string
}
type SportsCar struct {}
func (s SportsCar) Drive() string {
return "Vroom vroom!"
}
type SUV struct {}
func (s SUV) Drive() string {
return "Rumble rumble!"
}
func NewCar(carType string) Car {
if carType == "sports" {
return SportsCar{}
} else if carType == "suv" {
return SUV{}
}
return nil
}
myCar := NewCar("sports")
fmt.Println(myCar.Drive())
// Output: Vroom vroom!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment