github.com/flower-corp/rosedb@v1.1.2-0.20230117132829-21dc4f7b319a/examples/sets/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 err = db.SAdd([]byte("fruits"), []byte("watermelon"), []byte("grape"), []byte("orange"), []byte("apple")) 19 if err != nil { 20 fmt.Printf("SAdd error: %v", err) 21 } 22 23 err = db.SAdd([]byte("fav-fruits"), []byte("orange"), []byte("melon"), []byte("strawberry")) 24 if err != nil { 25 fmt.Printf("SAdd error: %v", err) 26 } 27 28 diffSet, err := db.SDiff([]byte("fruits"), []byte("fav-fruits")) 29 if err != nil { 30 fmt.Printf("SDiff error: %v", err) 31 } 32 fmt.Println("SDiff set:") 33 for _, val := range diffSet { 34 fmt.Printf("%v\n", string(val)) 35 } 36 37 unionSet, err := db.SUnion([]byte("fruits"), []byte("fav-fruits")) 38 if err != nil { 39 fmt.Printf("SUnion error: %v", err) 40 } 41 fmt.Println("\nSUnion set:") 42 for _, val := range unionSet { 43 fmt.Printf("%v\n", string(val)) 44 } 45 }