-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
47 lines (39 loc) · 1.04 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package main
import (
"fmt"
"github.com/syndtr/goleveldb/leveldb"
)
func main() {
// Create leveldb folder
db, err := leveldb.OpenFile("db", nil)
if err != nil {
_ = fmt.Errorf("error opening db: %v", err)
}
defer db.Close()
// modify database
if err = db.Put([]byte("key"), []byte("hello world"), nil); err != nil {
_ = fmt.Errorf("error inserting key: %v", err)
}
// modifu database
if err = db.Put([]byte("key"), []byte("hello and bye"), nil); err != nil {
_ = fmt.Errorf("error inserting key: %v", err)
}
// get key from database
value, err := db.Get([]byte("key"), nil)
if err != nil {
_ = fmt.Errorf("error getting key: %v", err)
}
fmt.Println("value: ", string(value))
// delete key from database
if err = db.Delete([]byte("key"), nil); err != nil {
_ = fmt.Errorf("error deleting key: %v", err)
} else {
fmt.Println("deleted key")
}
// get not exist key from database
value, err = db.Get([]byte("key"), nil)
if err != nil {
_ = fmt.Errorf("error getting key: %v", err)
}
fmt.Println("value: ", string(value))
}