Trouble using exported function type with C types as parameters

假如想象 提交于 2020-01-04 01:53:07

问题


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:


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



来源:https://stackoverflow.com/questions/31099620/trouble-using-exported-function-type-with-c-types-as-parameters

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!