Pingo —— Go 语言的插件化开发包

简介:

Pingo 是一个用来为 Go 程序编写插件的简单独立库,因为 Go 本身是静态链接的,因此所有插件都以外部进程方式存在。Pingo 旨在简化标准 RPC 包,支持 TCP 和 Unix 套接字作为通讯协议。当前还不支持远程插件,如果有需要,远程插件很快会提供。

使用 Pingo 创建一个插件非常简单,首先新建目录,如 "plugins/hello-world" ,然后在该目录下编写 main.go:

// Always create a new binary
package main

import "github.com/dullgiulio/pingo"

// 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 = "Hello, " + name
    return nil
}

func main() {
    plugin := &MyPlugin{}

    // Register the objects to be exported
    pingo.Register(plugin)
    // Run the main events handler
    pingo.Run()
}

编译:

$ cd plugins/hello-world
$ go build

接下来就可以调用该插件:

package main

import (
    "log"
    "github.com/dullgiulio/pingo"
)

func main() {
    // Make a new plugin from the executable we created. Connect to it via TCP
    p := pingo.NewPlugin("tcp", "plugins/hello-world/hello-world")
    // 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("MyPlugin.SayHello", "Go developer", &resp); err != nil {
        log.Print(err)
    } else {
        log.Print(resp)
    }
}

文章转载自 开源中国社区 [http://www.oschina.net]

相关文章
|
10天前
|
Go
go语言中的数据类型
go语言中的数据类型
11 0
|
16天前
|
Go 开发者
掌握Go语言:Go语言结构体,精准封装数据,高效管理实体对象(22)
掌握Go语言:Go语言结构体,精准封装数据,高效管理实体对象(22)
|
16天前
|
安全 Go
掌握Go语言:Go语言通道,并发编程的利器与应用实例(20)
掌握Go语言:Go语言通道,并发编程的利器与应用实例(20)
|
16天前
|
存储 缓存 安全
掌握Go语言:Go语言中的字典魔法,高效数据检索与应用实例解析(18)
掌握Go语言:Go语言中的字典魔法,高效数据检索与应用实例解析(18)
|
16天前
|
Go
使用Go语言发邮件
使用Go语言发邮件
20 2
|
28天前
|
缓存 安全 Java
Go语言小细节
Go语言小细节
36 0
|
16天前
|
存储 安全 Go
掌握Go语言:Go语言类型转换,无缝处理数据类型、接口和自定义类型的转换细节解析(29)
掌握Go语言:Go语言类型转换,无缝处理数据类型、接口和自定义类型的转换细节解析(29)
|
1天前
|
前端开发 Java Go
开发语言详解(python、java、Go(Golong)。。。。)
开发语言详解(python、java、Go(Golong)。。。。)
|
10天前
|
存储 Java 编译器
go语言基础语法
go语言基础语法
|
16天前
|
存储 缓存 安全
掌握Go语言:Go语言Map,高效键值对集合的应用与注意事项详解(26)
掌握Go语言:Go语言Map,高效键值对集合的应用与注意事项详解(26)