dongwo1234
2018-04-27 20:51
浏览 96
已采纳

将通道参数传递给golang中的动态加载函数

I'm new to golang and golang plugins. I'm having trouble passing a chan object to this functions. If I switch to int it works. Not sure what exactly I am missing. Thanks for any help.

dataunit.go

package main

type DataUnit struct {
   i int
   s string
}

modcounter.go

package main
import ( 
  "fmt"
//  "strconv"
)
type module string 
//func (m module) RunMod(i int) {
func (m module) RunMod(in <-chan *DataUnit) {
    //fmt.Println("Hello Universe " + strconv.Itoa(i))
    fmt.Println("Hello Universe ")
        n := <-in
        fmt.Printf(n.s)
}
var Module module

modmain.go

package main

import (
  "fmt"
  "os"
  "plugin"
)


type DataUnit struct {
   i int
   s string
}

type Module interface {
        //RunMod(i int)
        RunMod(in <-chan *DataUnit)
}


func main() {


        out := make(chan *DataUnit, 2000)
        plug, err := plugin.Open("./modcounter.so")
        if err != nil {
                fmt.Printf("FATAL (plugin.Open): " + err.Error())
                os.Exit(1)
        }

        symModule, err := plug.Lookup("Module")
        if err != nil {
           fmt.Printf(err.Error())
           os.Exit(1)
        }

        var module Module
        module, ok:= symModule.(Module)
        if !ok {
                fmt.Println("unexpected type from module symbol")
                os.Exit(1)
        }

        //module.RunMod(5)
        module.RunMod(out)
}

go build -buildmode=plugin -o modcounter.so modcounter.go dataunit.go
go build modmain.go dataunit.go

./modmain
unexpected type from module symbol

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

2条回答 默认 最新

相关推荐 更多相似问题