Skip to content

Instantly share code, notes, and snippets.

@arriqaaq
Created January 26, 2023 07:27
Show Gist options
  • Save arriqaaq/048203dfa967f78cd6686b3007e88b8a to your computer and use it in GitHub Desktop.
Save arriqaaq/048203dfa967f78cd6686b3007e88b8a 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 CarFactory(carType string) func() Car {
if carType == "sports" {
return func() Car { return SportsCar{} }
} else if carType == "suv" {
return func() Car { return SUV{} }
}
return nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment