-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
79 lines (60 loc) · 1.58 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package main
// package main
import (
"github.com/dgraph-io/badger"
)
func main() {
opts := badger.DefaultOptions("./data")
opts.Dir = "./data"
opts.ValueDir = "./data"
//opts.Logger = nil
//opts.Truncate = true
db, err := badger.Open(opts)
if err != nil {
panic(err)
}
defer db.Close()
// err = db.View(func(txn *badger.Txn) error {
// item, err := txn.Get([]byte("marlon1"))
// if err != nil {
// panic(err)
// }
// var valNot, valCopy []byte
// err = item.Value(func(val []byte) error {
// // This func with val would only be called if item.Value encounters no error.
// // Accessing val here is valid.
// fmt.Printf("The answer is: %s\n", val)
// // Copying or parsing val is valid.
// valCopy = append([]byte{}, val...)
// // Assigning val slice to another variable is NOT OK.
// valNot = val // Do not do this.
// return nil
// })
// if err != nil {
// panic(err)
// }
// // DO NOT access val here. It is the most common cause of bugs.
// fmt.Printf("NEVER do this. %s\n", valNot)
// // You must copy it to use it outside item.Value(...).
// fmt.Printf("The answer is: %s\n", valCopy)
// // Alternatively, you could also use item.ValueCopy().
// valCopy, err = item.ValueCopy(nil)
// if err != nil {
// panic(err)
// }
// fmt.Printf("The answer is: %s\n", valCopy)
// return nil
// })
// if err != nil {
// panic(err)
// }
txn := db.NewTransaction(true)
err = txn.Set([]byte("marlon1"), []byte("marlon-value1"))
if err != nil {
panic(err)
}
err = txn.Commit()
if err != nil {
panic(err)
}
}