github.com/flower-corp/rosedb@v1.1.2-0.20230117132829-21dc4f7b319a/examples/list/main.go (about) 1 package main 2 3 import ( 4 "fmt" 5 "github.com/flower-corp/rosedb" 6 "path/filepath" 7 ) 8 9 func main() { 10 path := filepath.Join("/tmp", "rosedb") 11 opts := rosedb.DefaultOptions(path) 12 db, err := rosedb.Open(opts) 13 if err != nil { 14 fmt.Printf("open rosedb err: %v", err) 15 return 16 } 17 18 // dataStruct: Ming, Jame, Tom 19 err = db.LPush([]byte("students"), []byte("Tom"), []byte("Jame"), []byte("Ming")) 20 if err != nil { 21 fmt.Printf("write data err: %v", err) 22 return 23 } 24 25 err = db.LPushX([]byte("not-exist"), []byte("Tom")) 26 fmt.Println(err) // ErrKeyNotFound 27 err = db.LPushX([]byte("students"), []byte("Rose")) 28 if err != nil { 29 fmt.Printf("write data err: %v", err) 30 return 31 } 32 33 // dataStruct: Ming, Jame, Tom, Jack, Wei 34 err = db.RPush([]byte("students"), []byte("Jack"), []byte("Wei")) 35 if err != nil { 36 fmt.Printf("write data err: %v", err) 37 return 38 } 39 40 err = db.RPushX([]byte("not-exist"), []byte("Jack")) 41 fmt.Println(err) // ErrKeyNotFound 42 err = db.RPushX([]byte("students"), []byte("Duan")) 43 if err != nil { 44 fmt.Printf("write data err: %v", err) 45 return 46 } 47 48 stuLens := db.LLen([]byte("students")) 49 fmt.Println(stuLens) 50 51 // out: Ming 52 // dataStruct: Jame, Tom, Jack, Wei 53 lPopStu, err := db.LPop([]byte("students")) 54 if err != nil { 55 fmt.Printf("lpop data err: %v", err) 56 return 57 } 58 fmt.Println(string(lPopStu)) 59 60 // out: Wei 61 // dataStruct: Jame, Tom, Jack 62 rPopStu, err := db.RPop([]byte("students")) 63 if err != nil { 64 fmt.Printf("rpop data err: %v", err) 65 return 66 } 67 fmt.Println(string(rPopStu)) 68 }