Első lépésben hozzunk létre egy cloud nevű packaget és definiáljuk a közös interfészt, amit meg kell valósítaniuk az egyes kiterjesztéseknek. A példában egyetlen metódus szerepel, a CloudPlugin.Validate().
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 CloudPlugin interface { | |
Validate() | |
} |
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 Provider string | |
var providers map[Provider]CloudPlugin = make(map[Provider]CloudPlugin) | |
func Register(provider Provider, plugin CloudPlugin) { | |
providers[provider] = plugin | |
} |
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
func Providers() (resp []Provider) { | |
for provider := range providers { | |
resp = append(resp, provider) | |
} | |
return | |
} | |
func GetProvider(provider Provider) CloudPlugin { | |
resp := providers[provider] | |
return resp | |
} |
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
const OPENSTACK = cloud.Provider("OPENSTACK") | |
type OpenstackPlugin struct { | |
} | |
func (p *OpenstackPlugin) Validate() { | |
println("openstack validator") | |
} |
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
func init() { | |
cloud.Register(OPENSTACK, new(OpenstackPlugin)) | |
} |
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 main | |
import "github.com/mhmxs/go-plugins-tutorial/cloud" | |
import _ "github.com/mhmxs/go-plugins-tutorial/cloud/openstack" | |
func main() { | |
for _, provider := range cloud.Providers() { | |
cloud.GetProvider(provider).Validate() | |
} | |
} |
# go run main.go openstack validatorEgy dologról szeretnék még szót ejteni. A cloud.Register() függvényt ha közelebbről megvizsgáljuk láthatjuk, hogy egyáltalán nem szál biztos, ami a példa program esetben nem okoz gondot. Viszont ha ismeretlen forrásból érkezhet regisztráció, akkor egy Mutexel garantálnunk kell a szálbiztos működést. Ezzel a témával a Go konkurencia kezelés gyorstalpaló cikkben foglalkoztam részletesebben.
A példaprogram teljes forráskódja a hiányzó RedHat implementációval együtt megtalálható GitHubon.