Trouble using exported function type with C types as parameters

459 views Asked by At

EDIT: changed the title to better reflect the problem thanks to @Not_a_Golfer

I have been experimenting with Go and cannot figure out this problem.

The following works fine when it is contained within the main package:

// Define a callback-function type, and define an invoker for it
type Callback func(i C.int, d C.double)
func Invoke(cb Callback, i C.int, d C.double) {
        cb(i, d)
}
//... then define my callback and pass it into the invoker
func foo(i C.int, d C.double) {
        fmt.Printf("i %d d %f\n", i, d)
}
func main() {
        Invoke(foo, 2, 2.2)
}

But when I define type Callback and Invoke in a package, import the package into main and call mypkg.Invoker(foo, 2, 2.2) I get the following build error:

src/hello/hello.go:9: cannot convert foo (type func(C.int, C.double)) to type mypkg.Callback

Edit: here's the main code for the hello package. I structured everything so that I can use the go command for build/install:

package main
import "C"
import "mypkg"
import "fmt"
func foo(i C.int, d C.double) {
        fmt.Printf("i %d d %f\n", i, d)
}
func main() {
        mypkg.Invoke( mypkg.Callback(foo), 2, 2.2 )
}

Anybody know if there's anything special to be done for a type exported from a package? I'm using C types in the arguments because eventually this will be a wrapper around a C-library, and I just wanted to be sure the C types were not part of the problem matching my callback function to the callback type.

Here's the package code:

package mypkg
import "C"
import "fmt"
type Callback func(i C.int, d C.double)
func Invoke(cb Callback, i C.int, d C.double) {
        fmt.Println("Calling cb\n")
        cb( i, d )
}
1

There are 1 answers

0
chendesheng On

They are actually different types!

Change your code to:

package main

import "C"
import "mypkg"

func main() {
    a := 1

    mypkg.Inc(C.int(a))
}

mypkg:

package mypkg

import "C"

func Inc(a C.int) C.int {
    return a + 1
}

Compile first package will get error: ./test.go:9: cannot use C.int(a) (type C.int) as type mypkg.C.int in argument to mypkg.Inc