// Stores data for a specified key. Any existing data for the key will be replaced. // If `data` is nil, then the key will be deleted if it exists in the database. func (d *Database) Store(key, data []byte) { dkey := toDatum(key) if data == nil { C.gdbm_delete(d.dbf, dkey) } else { C.gdbm_store(d.dbf, dkey, toDatum(data), C.GDBM_REPLACE) } }
func (d *gdbm) Store(keydata, valuedata []byte) error { key := toDatum(keydata) if valuedata == nil { C.gdbm_delete(d.dbf, key) return nil } value := toDatum(valuedata) C.gdbm_store(d.dbf, key, value, C.GDBM_REPLACE) return nil }
// Internal helper method to hide the two constants GDBM_INSERT and // GDBM_REPLACE from the user. func (db *Database) update(key string, value string, flag C.int) (err error) { // Convert key and value into libgdbm's `datum` data structure. See the // C definition at the top for the implementation of C.mk_datum(string). kcs := C.CString(key) vcs := C.CString(value) k := C.mk_datum(kcs) v := C.mk_datum(vcs) defer C.free(unsafe.Pointer(kcs)) defer C.free(unsafe.Pointer(vcs)) retv := C.gdbm_store(db.dbf, k, v, flag) if retv != 0 { err = lastError() } return err }