Question about plugin.Symbol - types from different scope

Hi,

I’m trying to use the plugin package to dynamically load a function definition during runtime. The function works with a struct.

I’m getting a weird error when running my program.

panic: interface conversion: plugin.Symbol is func(main._Ctype_struct_Vertex), not func(main._Ctype_struct_Vertex) (types from different scopes)

Here are my programs and steps to reproduce the error.
In vertex.go

package main

/*
struct Vertex {
    int x;
    int y;
};
*/
import "C"
import (
	"fmt"
)

//export Print
func Print(v C.struct_Vertex) {
	fmt.Printf("v.x = %d, v.y = %d\n", v.x, v.y)
}

func main() {}

and I compiled it via:
go build -buildmode=plugin -o libvertex.so vertex.go

In my main.go

package main

/*
struct Vertex {
    int x;
    int y;
};
*/
import "C"
import (
	"fmt"
	"os"
	"plugin"
)

var fp printFunc

type printFunc func(C.struct_Vertex)

func main() {

	plug, err := plugin.Open(string("./libvertex.so"))

	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}

	print, err := plug.Lookup("Print")

	fp = print.(func(C.struct_Vertex))
	fp(C.struct_Vertex{x: 1, y: 2})
}

Then, when I run go run main.go. I got this error:
panic: interface conversion: plugin.Symbol is func(main._Ctype_struct_Vertex), not func(main._Ctype_struct_Vertex) (types from different scopes)

Any idea why I’m getting this error? I know it probably has something to do with the duplicate definition in two files, and I tried to use a c header file, but still did not work.

Thanks in advance!

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.