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,

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


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.


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

Discussion Posts


Follow ups

Related Discussions

Discussion Navigation
viewthread | post
posts ‹ prev | 3 of 4 | next ›
Discussion Overview
groupgolang-nuts @
postedSep 29, '13 at 5:01p
activeSep 30, '13 at 3:32a



site design / logo © 2021 Grokbase