Pingo Go 的插件化开发 Pingo

polaris • 2997 次点击    
这是一个分享于 的项目,其中的信息可能已经有所发展或是发生改变。
Pingo 是一个用来为 Go 程序编写插件的简单独立库,因为 Go 本身是静态链接的,因此所有插件都以外部进程方式存在。Pingo 旨在简化标准 RPC 包,支持 TCP 和 Unix 套接字作为通讯协议。当前还不支持远程插件,如果有需要,远程插件很快会提供。 使用 Pingo 创建一个插件非常简单,首先新建目录,如 &#34;plugins/hello-world&#34; ,然后在该目录下编写 main.go: <pre class="brush:cpp ;toolbar: true; auto-links: false;">// Always create a new binary package main import &#34;github.com/dullgiulio/pingo&#34; // Create an object to be exported type MyPlugin struct{} // Exported method, with a RPC signature func (p *MyPlugin) SayHello(name string, msg *string) error {     *msg = &#34;Hello, &#34; + name     return nil } func main() {     plugin := &amp;MyPlugin{}     // Register the objects to be exported     pingo.Register(plugin)     // Run the main events handler     pingo.Run() }</pre> 编译: <pre class="brush:shell;toolbar: true; auto-links: false;">$ cd plugins/hello-world $ go build</pre> 接下来就可以调用该插件: <pre class="brush:cpp ;toolbar: true; auto-links: false;">package main import (     &#34;log&#34;     &#34;github.com/dullgiulio/pingo&#34; ) func main() {     // Make a new plugin from the executable we created. Connect to it via TCP     p := pingo.NewPlugin(&#34;tcp&#34;, &#34;plugins/hello-world/hello-world&#34;)     // Actually start the plugin     p.Start()     // Remember to stop the plugin when done using it     defer p.Stop()     var resp string     // Call a function from the object we created previously     if err := p.Call(&#34;MyPlugin.SayHello&#34;, &#34;Go developer&#34;, &amp;resp); err != nil {         log.Print(err)     } else {         log.Print(resp)     } }</pre>
授权协议:
MIT
开发语言:
Google Go 查看源码»
操作系统:
跨平台
2997 次点击  
加入收藏 微博
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传