FAQ
I need to wrap a C++ library that returns an array of strings.

In the current solution, I export the Go closure append_internal to be
called from the C library wrapper.

Is the code below the proper way to do it or is there a simpler way ?

thanks

/*
#include <stdlib.h>
void* new_db(const char* file);
void get_results(void *p, const char* key);
*/
import "C"

import (
         "unsafe"
)


type DB struct {
         ptr unsafe.Pointer
}

func Open(file string) DB {
         cStr := C.CString(file)
         defer C.free(unsafe.Pointer(cStr))

         p := C.new_db(cStr)
         return DB{p}
}

var Results = []string{}

//export append_internal
func append_internal(s *C.char, l C.int) {
         str := C.GoStringN(s, l)
         Results = append(Results, str)
}

func (db DB) GetResults(q string) []string {
         Results = Results[:0]
         cStr := C.CString(q)
         defer C.free(unsafe.Pointer(cStr))

         C.get_results(db.ptr, cStr)

         return Results
}

func (db DB) Close() {
         C.free(db.ptr)
}

--
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

  • Minux at Nov 17, 2013 at 12:49 am

    On Wed, Nov 13, 2013 at 6:46 AM, Jose wrote:

    I need to wrap a C++ library that returns an array of strings.

    In the current solution, I export the Go closure append_internal to be
    called from the C library wrapper.
    this is ok, but not very efficient.
    Is the code below the proper way to do it or is there a simpler way ?
    you can just make the code return a C array of string pointers, and use
    this to convert it to a Go slice
    and then range loop thru. it to convert individual *C.char to Go string.

    http://play.golang.org/p/XtHIUSBhgL
    thanks

    /*
    #include <stdlib.h>
    void* new_db(const char* file);
    void get_results(void *p, const char* key);
    */
    import "C"

    import (
    "unsafe"
    )


    type DB struct {
    ptr unsafe.Pointer
    }

    func Open(file string) DB {
    cStr := C.CString(file)
    defer C.free(unsafe.Pointer(cStr))

    p := C.new_db(cStr)
    return DB{p}
    }

    var Results = []string{}

    //export append_internal
    func append_internal(s *C.char, l C.int) {
    str := C.GoStringN(s, l)
    Results = append(Results, str)
    }

    func (db DB) GetResults(q string) []string {
    Results = Results[:0]
    cStr := C.CString(q)
    defer C.free(unsafe.Pointer(cStr))

    C.get_results(db.ptr, cStr)

    return Results
    }

    func (db DB) Close() {
    C.free(db.ptr)
    }


    --
    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.
    --
    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
postedNov 13, '13 at 11:46a
activeNov 17, '13 at 12:49a
posts2
users2
websitegolang.org

2 users in discussion

Jose: 1 post Minux: 1 post

People

Translate

site design / logo © 2021 Grokbase