Pigo是一个用来为Go程序编写插件的简单独立库,因为Go本身是静态链接的,因此所有插件都以外部进程方式存在。Pigo旨在简化标准RPC包,支持TCP和Uix套接字作为通讯协议。当前还不支持远程插件,如果有需要,远程插件很快会提供。
使用Pigo创建一个插件非常简单,首先新建目录,如"plugis/hello-world",然后在该目录下编写mai.go:
// Always create a ew biarypackage maiimport "github.com/dullgiulio/pigo"// Create a object to be exportedtype MyPlugi struct{}// Exported method, with a RPC sigaturefuc (p *MyPlugi) SayHello(ame strig, msg *strig) error { *msg = "Hello, " + ame retur il}fuc mai() { plugi := &MyPlugi{} // Register the objects to be exported pigo.Register(plugi) // Ru the mai evets hadler pigo.Ru()}编译:
$ cd plugis/hello-world$ go build接下来就可以调用该插件:
package maiimport ( "log" "github.com/dullgiulio/pigo")fuc mai() { // Make a ew plugi from the executable we created. Coect to it via TCP p := pigo.NewPlugi("tcp", "plugis/hello-world/hello-world") // Actually start the plugi p.Start() // Remember to stop the plugi whe doe usig it defer p.Stop() var resp strig // Call a fuctio from the object we created previously if err := p.Call("MyPlugi.SayHello", "Go developer", &resp); err != il { log.Prit(err) } else { log.Prit(resp) }}
评论