Help in writing Go wrappers to a C libarary [beginner question]

710 views
Skip to first unread message

Dar

unread,
Sep 29, 2013, 4:04:05 AM9/29/13
to golan...@googlegroups.com
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


Miki Tebeka

unread,
Sep 29, 2013, 1:25:15 PM9/29/13
to golan...@googlegroups.com
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. 

Craig Mason-Jones

unread,
Sep 29, 2013, 11:31:48 PM9/29/13
to golan...@googlegroups.com
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

Dar

unread,
Sep 29, 2013, 11:32:22 PM9/29/13
to golan...@googlegroups.com
Thanks!
Reply all
Reply to author
Forward
0 new messages