Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Good or recommended way to translate C struct to Go struct

Tags:

go

cgo

i'm using cgo for developing library binding from Go. Let me consider the C struct and Go Struct as below.

struct cons_t {
  size_t type;
  cons_t *car;
  cons_t *cdr;
};

cons_t* parse(const char *str);

and this is go's struct

type Cons struct {
  type int;
  car *Cons;
  cdr *Cons;
}

For implementing Go function as below, what is better way to implement TranslateCCons2GoCons?

func Parse (str string) *Cons {
  str_ptr := C.CString(string);
  cons_ptr := C.parse(str_ptr);
  retCons := TranslateCCons2GoCons(cons_ptr);
  return retCons;
}

My first answer is as below.

/*#cgo
int getType(cons_t *cons) {
    return cons->type;
}
cons_t *getCar(cons_t *cons) {
  return cons->car;
}
cons_t *getCdr(cons_t *cons) {
  return cons->cdr;
}
*/

func TranslateCCons2GoCons (c *C.cons_t) Cons {
  type := C.getType(c);
  car := C.getCar(c);
  cdr := C.getCdr(c);
  // drop null termination for simplicity
  return Cons{type, TranslateCCons2GoCons(car), TranslateCCons2GoCons(cdr)};
}

are there any better way?

like image 944
shinpei Avatar asked Apr 11 '14 05:04

shinpei


2 Answers

You can use C structs in Go (though if the struct holds a union it gets a bit more complex). The simplest way would just be

type Cons struct {
    c C.cons_t
}

Any function in C is now just a passthrough in Go

func Parse(s string) Cons {
    str := C.CString(s)
    // Warning: don't free this if this is stored in the C code
    defer C.free(unsafe.Pointer(str))
    return Cons{c: C.parse(str)}
}

This has its own overhead, since you have to do a type conversion on element access. So what was before var c Cons{}; c.Type is now

func (c Cons) Type() int {
    return int(c.c.type)
}

An intermediate compromise can be used where you store fields alongside the C type for easy access

type Cons struct {
    type int
    c C.cons_t
}

func (c *Cons) SetType(t int) {
    c.type = t
    c.c.type = C.size_t(t)
}

func (c Cons) Type() int {
    return c.type
}

The only real problem with this is that if you're calling C functions a lot, this can introduce maintenance overhead in setting the Go-side fields:

func (c *Cons) SomeFuncThatAltersType() {
    C.someFuncThatAltersType(&c.c)
    c.Type = int(c.c.type) // now we have to remember to do this
}
like image 195
Linear Avatar answered Oct 11 '22 05:10

Linear


I would recommend against the accessor functions. You should be able to access the fields of the C struct directly, which will avoid the Go -> C function call overhead (which is non-trivial). So you might use something like:

func TranslateCCons2GoCons (c *C.cons_t) *Cons {
    if c == nil {
        return nil
    }
    return &Cons{
        type: int(c.type),
        car: TranslateCCons2GoCons(c.car),
        cdr: TranslateCCons2GoCons(c.cdr),
    }
}

Also, if you allocate a C string with C.CString, you need to free it. So your Parse function should look something like:

func Parse (str string) *Cons {
    str_ptr := C.CString(str)
    defer C.free(unsafe.Pointer(str_ptr)
    cons_ptr := C.parse(str_ptr)
    retCons := TranslateCCons2GoCons(cons_ptr)
    // FIXME: Do something to free cons_ptr here.  The Go runtime won't do it for you
    return retCons
}
like image 20
James Henstridge Avatar answered Oct 11 '22 05:10

James Henstridge