Created
January 28, 2015 21:39
-
-
Save nesv/1fbb5f3e41c4ecc1b986 to your computer and use it in GitHub Desktop.
Factory pattern in Go (potentially?)
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
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