golang插件

本文记载下了golang插件的用法, 原文:

https://code.tutsplus.com/tutorials/writing-plugins-in-go--cms-29101

 

这个例子使用了两个排序: 快速排序和冒泡排序,不追求算法的高效,只为了说明插件的生成和使用。

而且只在linux下,build和运行.(windows环境没有测试过, 原文在windows下要使用docker)

目录:

plugins
├── bubble_sort
│   └── bubble_sort_plugin.go   冒泡排序
├── bubble_sort_plugin.so       编译后的插件
├── quick_sort
│   └── quick_sort_plugin.go     快速排序
├── quick_sort_plugin.so          编译后的插件
├── sort                                    编译后的主程序
└── sort.go                               main()                  

bubble_sort_plugin.go

package main


func Sort(items []int) *[]int {
    if len(items) < 2 {
        return &items
    }

    tmp := 0

    for i := 0; i < len(items); i++ {
        for j := 0; j < len(items)-1; j++ {
            if items[j] > items[j+1] {
                tmp = items[j]
                items[j] = items[j+1]
                items[j+1] = tmp
            }
        }
    }

    return &items
}

 

quick_sort_plugin.go

package main

import "math/rand"


func Sort(items []int) *[]int {
    if len(items) < 2 {
        return &items
    }

    peg := items[rand.Intn(len(items))]

    below := make([]int, 0, len(items))
    above := make([]int, 0, len(items))
    middle := make([]int, 0, len(items))

    for _, item := range items {
        switch {
        case item < peg:
            below = append(below, item)
        case item == peg:
            middle = append(middle, item)
        case item > peg:
            above = append(above, item)
        }
    }


    below = *Sort(below)
    above = *Sort(above)

    sorted := append(append(below, middle...), above...)
    return &sorted
}

 

sort.go

package main

import (
        "fmt"
        "plugin"
        "path/filepath"
)

func main() {
        numbers := []int{5,2,7,6,1,3,4,8}

        all_plugins, err := filepath.Glob("*.so")
        if err != nil {
                panic(err)
        }

        for _, filename := range (all_plugins) {
                p, err := plugin.Open(filename)  // 加载插件
                if err != nil {
                        panic(err)
                }

                symbol, err := p.Lookup("Sort")  // 查找插件里的指定函数
                if err != nil {
                        panic(err)
                }

                sortFunc, ok := symbol.(func([]int) *[]int)

                if !ok {
                        panic("Plugin has no 'Sort([]int) *[]int)' function")
                }

                sorted := sortFunc(numbers)      // 调用插件函数
                fmt.Println(filename, sorted)
        }
}

 

编译:

 编译 bubble_sort_plugin.go:

     在bubble_sort目录里:  

# go build -buildmode=plugin -o ../bubble_sort_plugin.so 

      生成的quick_sort_plugin.so在plugins目录里

 

同样:

    在quick_sort目录里:  

go build -buildmode=plugin -o ../quick_sort_plugin.so

    

编译sort.go:

    在plugins目录里:

 # go build sort.go 

    生成 sort (绿色,可执行)

 

执行sort:

# ./sort
bubble_sort_plugin.so &[1 2 3 4 5 6 7 8]
quick_sort_plugin.so &[1 2 3 4 5 6 7 8]

 

posted @ 2018-05-05 17:24  北极熊129  阅读(911)  评论(0编辑  收藏  举报