SHARE
TWEET

Untitled

a guest Sep 16th, 2019 85 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package main
  2.  
  3. import (
  4.     "fmt"
  5. )
  6.  
  7. type Creator func() Plugin
  8.  
  9. type Plugin interface {
  10.     Pr()
  11. }
  12.  
  13. var Plugins = map[string]Creator{}
  14.  
  15. func Add(name string, creator Creator) {
  16.     Plugins[name] = creator
  17. }
  18.  
  19. func CreatePluginStruct(creator Creator) Plugin {
  20.     return creator()
  21. }
  22.  
  23. func main() {
  24.     RedisInt()
  25.     name := "redis"
  26.     creator := Plugins[name]
  27.     newstruct := CreatePluginStruct(creator)
  28.     newstruct.Pr()
  29.  
  30. }
  31.  
  32. //以下是另一个文件
  33.  
  34. type Redis struct {
  35.     Name string
  36. }
  37.  
  38. func (r Redis) Pr() {
  39.     fmt.Println(r.Name)
  40. }
  41.  
  42. //模拟init()
  43. func RedisInt() {
  44.     Add("redis", func() Plugin {
  45.         return &Redis{Name: "redis"}
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top