gpt4 book ai didi

go - 将 channel 参数传递给 golang 中的动态加载函数

转载 作者:IT王子 更新时间:2023-10-29 01:57:05 24 4
gpt4 key购买 nike

我是 golang 和 golang 插件的新手。我无法将 chan 对象传递给此函数。如果我切换到 int 它会起作用。不确定我到底错过了什么。感谢您的帮助。

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
去构建 modmain.go dataunit.go

./modmain
来自模块符号的意外类型

最佳答案

如果您仍在学习 golang,那么绝对不是从插件开始的地方。我相信插件支持仍处于试验阶段,并不适用于所有操作系统 - 在 1.10 中,我相信 Darwin 已添加到以前的 Linux 中。

快速搜索会发现一个与您的问题类似的预先存在的问题报告 - 看起来它在 go 1.10 中: https://github.com/golang/go/issues/24351

您也许可以跟踪存储库并获得修复此问题的分支或版本,但是在您学习的同时,您不会对这一功能领域的问题是您的代码还是插件系统有任何信心.因此,我建议现在坚持学习核心语言功能。

关于go - 将 channel 参数传递给 golang 中的动态加载函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50070131/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com