github.com/rosedblabs/rosedb/v2@v2.3.7-0.20240423093736-a89ea823e5b9/examples/basic/main.go (about)

     1  package main
     2  
     3  import (
     4  	"os"
     5  	"runtime"
     6  
     7  	"github.com/rosedblabs/rosedb/v2"
     8  )
     9  
    10  // this file shows how to use the basic operations of rosedb
    11  
    12  func main() {
    13  
    14  	// specify the options
    15  	options := rosedb.DefaultOptions
    16  	sysType := runtime.GOOS
    17  	if sysType == "windows" {
    18  		options.DirPath = "C:\\rosedb_basic"
    19  	} else {
    20  		options.DirPath = "/tmp/rosedb_basic"
    21  	}
    22  
    23  	//remove data dir, for test, there's no need to keep any file or directory on disk
    24  	defer func() {
    25  		_ = os.RemoveAll(options.DirPath)
    26  	}()
    27  
    28  	// open a database
    29  	db, err := rosedb.Open(options)
    30  	if err != nil {
    31  		panic(err)
    32  	}
    33  	defer func() {
    34  		_ = db.Close()
    35  	}()
    36  
    37  	// set a key
    38  	err = db.Put([]byte("name"), []byte("rosedb"))
    39  	if err != nil {
    40  		panic(err)
    41  	}
    42  
    43  	// get a key
    44  	val, err := db.Get([]byte("name"))
    45  	if err != nil {
    46  		panic(err)
    47  	}
    48  	println(string(val))
    49  
    50  	// delete a key
    51  	err = db.Delete([]byte("name"))
    52  	if err != nil {
    53  		panic(err)
    54  	}
    55  }