FAQ
Hello,

I have been trying to write Go bindings to an existing open source C
library. For this, I've read most of the material on CGo available online.
I've also done some small time dummy usage of C functions in Go using CGo.
However, I wanted to know the best practices to follow while doing so.

The library that I'm planning to write Go bindings has exiting ports in
Python using ctype and in Ruby as well. I want to know if they can be of
any use. More importantly I would be thankful if you could provide
suggestion for writing the wrapper.

The current library source consists of about 40 C files including a main
file. My question is that do I
1) write wrappers only for the main file, thus providing a Go layer on top
the existing C layer and provide an Go based interaction point to the
library (if at all this is possible) ?
2) Have the existing C lib exist as a dependency and write Go wrappers on
top of it (on the lines of the Python implementation, where ctypes has been
used this way). ?
3) Re write parts of the original C based library in Go, making maximum use
of its C implementation ?
4) Completely re-write the library in Go (not possible in the interest of
time) ?

Also, it would be nice if you could point me to an existing library (on
Google Code or Github) that provides Go bindings to an existing C library.

Thanks


--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Search Discussions

  • Miki Tebeka at Sep 29, 2013 at 5:25 pm

    The current library source consists of about 40 C files including a main
    file. My question is that do I
    1) write wrappers only for the main file, thus providing a Go layer on top
    the existing C layer and provide an Go based interaction point to the
    library (if at all this is possible) ?
    2) Have the existing C lib exist as a dependency and write Go wrappers on
    top of it (on the lines of the Python implementation, where ctypes has been
    used this way). ?
    3) Re write parts of the original C based library in Go, making maximum
    use of its C implementation ?
    4) Completely re-write the library in Go (not possible in the interest of
    time) ?
    I'd say go with option 2 if the library is easily installed with most
    package managers, otherwise probably option 1.
    When you wrap, try to make the Go API feels more natural in Go, even if you
    need to "bend" the C-API a bit.

    >
    Also, it would be nice if you could point me to an existing library (on
    Google Code or Github) that provides Go bindings to an existing C library.
    https://bitbucket.org/tebeka/snowball IMO there are several wrapper around
    SQLite out there as well.

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Dar at Sep 30, 2013 at 3:32 am
    Thanks!
    On Sunday, September 29, 2013 10:55:15 PM UTC+5:30, Miki Tebeka wrote:

    The current library source consists of about 40 C files including a main
    file. My question is that do I
    1) write wrappers only for the main file, thus providing a Go layer on
    top the existing C layer and provide an Go based interaction point to the
    library (if at all this is possible) ?
    2) Have the existing C lib exist as a dependency and write Go wrappers on
    top of it (on the lines of the Python implementation, where ctypes has been
    used this way). ?
    3) Re write parts of the original C based library in Go, making maximum
    use of its C implementation ?
    4) Completely re-write the library in Go (not possible in the interest of
    time) ?
    I'd say go with option 2 if the library is easily installed with most
    package managers, otherwise probably option 1.
    When you wrap, try to make the Go API feels more natural in Go, even if
    you need to "bend" the C-API a bit.

    Also, it would be nice if you could point me to an existing library (on
    Google Code or Github) that provides Go bindings to an existing C library.
    https://bitbucket.org/tebeka/snowball IMO there are several wrapper
    around SQLite out there as well.
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Craig Mason-Jones at Sep 30, 2013 at 3:31 am
    Hi,

    I wouldn't rewrite the C library unless you've got a compelling reason to
    do so. If it's an established library, it's most likely reliable, and C is
    probably faster than Go, so there's little to be gained in a rewrite.

    Integrating Go and C is really simple. I suggest you look at my Gophia
    binding of the Sophia library: http://www.github.com/craigmj/gophia. I read
    about Sophia over coffee in the morning, and had learned Go-C integration
    and written the binding by lunch. I would say that it's probably not the
    best binding in the world, but to be honest, the binding process was so
    simple, that I can't really see where I could have gone wrong.

    The only points that anything other than elementary are memory handling,
    especially with C strings, and calling C-variadic functions from Go: I
    wrote small C wrapper functions to call each 'variation' of the underlying
    function - the same solution that andelf resorted to with his/her gocurl
    library: https://github.com/andelf/go-curl (see easy.go).

    Have fun. I'm sure you'll join me in thanking the gods of Go for making C
    integration so straightfoward.

    All the best,
    Craig

    On Sunday, September 29, 2013 10:04:05 AM UTC+2, Dar wrote:

    Hello,

    I have been trying to write Go bindings to an existing open source C
    library. For this, I've read most of the material on CGo available online.
    I've also done some small time dummy usage of C functions in Go using CGo.
    However, I wanted to know the best practices to follow while doing so.

    The library that I'm planning to write Go bindings has exiting ports in
    Python using ctype and in Ruby as well. I want to know if they can be of
    any use. More importantly I would be thankful if you could provide
    suggestion for writing the wrapper.

    The current library source consists of about 40 C files including a main
    file. My question is that do I
    1) write wrappers only for the main file, thus providing a Go layer on top
    the existing C layer and provide an Go based interaction point to the
    library (if at all this is possible) ?
    2) Have the existing C lib exist as a dependency and write Go wrappers on
    top of it (on the lines of the Python implementation, where ctypes has been
    used this way). ?
    3) Re write parts of the original C based library in Go, making maximum
    use of its C implementation ?
    4) Completely re-write the library in Go (not possible in the interest of
    time) ?

    Also, it would be nice if you could point me to an existing library (on
    Google Code or Github) that provides Go bindings to an existing C library.

    Thanks

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedSep 29, '13 at 5:01p
activeSep 30, '13 at 3:32a
posts4
users3
websitegolang.org

People

Translate

site design / logo © 2021 Grokbase