Go 实现watch dog的样板
package main
import (
"fmt"
)
func main() {
wd := NewWatchDog()
wd.Register("price", NewPrice())
wd.Register("quote", NewQuote())
wd.Run()
}
type Dog interface {
Start()
}
type WatchDog struct {
}
var dogMap = map[string]Dog{}
func NewWatchDog() *WatchDog {
dogMap = make(map[string]Dog)
return &WatchDog{}
}
func (wd *WatchDog) Register(key string, dog Dog) {
dogMap[key] = dog
}
func (wd *WatchDog) Run() {
for key := range dogMap {
dog := dogMap[key]
dog.Start()
}
}
//==============price 服务
type price struct {
}
func NewPrice() Dog {
return &price{}
}
func (p *price) Start() {
fmt.Println("Here is a price instanace")
}
//==============quote服务
type quote struct {
}
func NewQuote() Dog {
return "e{}
}
func (q *quote) Start() {
fmt.Println("Here is a quote instanace")
}