Skip to content

Instantly share code, notes, and snippets.

@nesv
Created January 28, 2015 21:39
Show Gist options
  • Save nesv/1fbb5f3e41c4ecc1b986 to your computer and use it in GitHub Desktop.
Save nesv/1fbb5f3e41c4ecc1b986 to your computer and use it in GitHub Desktop.
Factory pattern in Go (potentially?)
package provider
// Some empty types for "completeness".
type (
VMConfig struct{}
VM struct{}
)
type InfraProvider interface {
Launch(cfg VMConfig) (*VM, error)
Destroy(vm *VM) error
}
func New(providerName string) (InfraProvider, error) {
switch providerName {
case "openstack":
return &openstackProvider, nil
case "aws":
return &awsProvider, nil
}
return fmt.Errorf("unknown provider name: %s", providerName)
}
// Implementation of a provider, when dealing with OpenStack.
type openstackProvider struct{}
func (p *openstackProvider) Launch(cfg VMConfig) (*VM, error) {
return &VM{}, nil
}
func (p *openstackProvider) Destroy(vm *VM) error {
return nil
}
// Implementation of a provider, when dealing with AWS.
type awsProvider struct{}
func (p *awsProvider) Launch(cfg VMConfig) (*VM, error) {
return &VM{}, nil
}
func (p *awsProvider) Destroy(vm *VM) error {
return nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment