FAQ

Search Discussions

  • Jason Del Ponte at Jan 22, 2013 at 5:55 am
    If a method is exported, it should be callable from C, but keep in mind all
    input and output parameters must be
    C compatible types. http://golang.org/cmd/cgo/ near the bottom goes into
    how go functions can be exported.

    When using the cgo you can do the following to allow a go function to be
    called from C

    //export myGoFuncToCallFromC
    func myGoFuncToCallFromC(p unsafe.Pointer, a *C.char) *C.char {
    ...
    }

    An example of this for c calls in the same file as the
    export, http://golang.org/misc/cgo/test/callback.go

    The exported method's address needs to be exposed to the C layer though, An
    example of
    this: https://github.com/jasondelponte/go-v8/blob/master/v8.go#L4. The
    v8_init is a c function defined in a c source file. The local helper
    method in the go file is to easily dereference the address of the function
    without it getting lost in the GC. Would love to know if there is a similar
    way to do that though, which doesn't require the helper method.

    Cheers,
    Jason

    On Monday, January 21, 2013 9:39:02 PM UTC-8, Robert Sandra wrote:

    just curious..
    --
  • Robert Sandra at Jan 22, 2013 at 6:21 am
    Thanks very much.

    Suppose I define a struct in Go, can I export it to C, and then use this
    type in C?

    Thank you.
    On Tuesday, January 22, 2013 12:55:26 AM UTC-5, Jason Del Ponte wrote:

    If a method is exported, it should be callable from C, but keep in mind
    all input and output parameters must be C compatible types.
    http://golang.org/cmd/cgo/ near the bottom goes into how go functions can
    be exported.

    When using the cgo you can do the following to allow a go function to be
    called from C

    //export myGoFuncToCallFromC
    func myGoFuncToCallFromC(p unsafe.Pointer, a *C.char) *C.char {
    ...
    }

    An example of this for c calls in the same file as the export,
    http://golang.org/misc/cgo/test/callback.go

    The exported method's address needs to be exposed to the C layer though,
    An example of this:
    https://github.com/jasondelponte/go-v8/blob/master/v8.go#L4. The v8_init
    is a c function defined in a c source file. The local helper method in the
    go file is to easily dereference the address of the function without it
    getting lost in the GC. Would love to know if there is a similar way to do
    that though, which doesn't require the helper method.

    Cheers,
    Jason

    On Monday, January 21, 2013 9:39:02 PM UTC-8, Robert Sandra wrote:

    just curious..
    --
  • Ian Lance Taylor at Jan 22, 2013 at 6:33 am

    On Mon, Jan 21, 2013 at 10:21 PM, Robert Sandra wrote:

    Suppose I define a struct in Go, can I export it to C, and then use this
    type in C?
    That does not work at present. Cgo supports exporting functions, but
    has no support for exporting types.

    Ian

    --

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedJan 22, '13 at 5:39a
activeJan 22, '13 at 6:33a
posts4
users3
websitegolang.org

People

Translate

site design / logo © 2022 Grokbase