Search code examples
cgoglib

Convert glib C method with string array as parameter to Go


I am trying to add some methods to the gotk3/glib Go repository, but I am having troubles converting the method g_settings_set_strv to Go; the C signature is this:

gboolean
g_settings_set_strv (GSettings *settings,
                     const gchar *key,
                     const gchar *const *value);

And this is what I have in Go (adapted copy paste, I am not a C guy, only Java, Python, etc.):

// SetStrv is a wrapper around g_settings_set_strv().
func (v *Settings) SetStrv(name string, values []string) bool {
    cstr1 := (*C.gchar)(C.CString(name))
    defer C.free(unsafe.Pointer(cstr1))

    cvalues := C.make_strings(C.int(len(values) + 1))
    defer C.destroy_strings(cvalues)

    for i, accel := range values {
        cstr := C.CString(accel)
        defer C.free(unsafe.Pointer(cstr))
        C.set_string(cvalues, C.int(i), (*C.gchar)(cstr))
    }
    C.set_string(cvalues, C.int(len(values)), nil)
    return gobool(C.g_settings_set_strv(v.native(), cstr1, cvalues))
}

But build fails with this message:

./settings.go:246: cannot use (*C.gchar)(cstr) (type *C.gchar) as type *C.char in argument to func literal
./settings.go:249: cannot use cvalues (type **C.char) as type **C.gchar in argument to func literal

I am stuck in this...


Solution

  • This code works and does what is supposed to:

    // SetStrv is a wrapper around g_settings_set_strv().
    func (v *Settings) SetStrv(name string, values []string) bool {
        cstr1 := (*C.gchar)(C.CString(name))
        defer C.free(unsafe.Pointer(cstr1))
    
        // cvalues := C.make_strings(C.int(len(values) + 1))
        cvalues := make([]*C.gchar, len(values))
    
        for i, accel := range values {
            cvalues[i] = (*C.gchar)(C.CString(accel))
            defer C.free(unsafe.Pointer(cvalues[i]))
        }
        cvalues = append(cvalues, nil)
    
        // C.set_string(cvalues, C.int(len(values)), nil)
        return gobool(C.g_settings_set_strv(v.native(), cstr1, &cvalues[0]))
    }
    

    Thanks to tommyshem from github for his help (commit fix).