内容简介:package mainimport ("fmt"
package main
import (
"fmt"
"github.com/syndtr/goleveldb/leveldb"
)
func main() {
levelDB()
}
func levelDB() {
//定义字符数组key value
key := []byte("Test")
value := []byte("Test Level DB")
//定义字符串的键值对
k1 :="key"
v1 :="value"
// The returned DB instance is safe for concurrent use.
// The DB must be closed after use, by calling Close method.
//文件夹位置,获取db实例-设置数据库文件的位置
db, err := leveldb.OpenFile("path/to/db", nil)
//leveldb.
//延迟关闭db流,必须的操作
defer db.Close()
if err != nil {
fmt.Println(err)
}
//向db中插入键值对
e := db.Put(key, value, nil)
//将字符串转换为byte数组
e = db.Put([]byte(k1), []byte(v1), nil)
fmt.Println(e)//
/**
|| 注意:查看路径下的db文件,可知道文件都是自动切分为一些小文件.
*/
//根据key值查询value
data, _ := db.Get(key, nil)
fmt.Println(data)//[84 101 115 116 32 76 101 118 101 108 32 68 66]
fmt.Printf("%c\n",data)//[T e s t L e v e l D B]
data, _ = db.Get([]byte(k1), nil)
fmt.Println(data)//[118 97 108 117 101]
fmt.Printf("%c\n",data)//[v a l u e]
// It is safe to modify the contents of the arguments after Delete returns but
// not before.
//根据key进行删除操作
i := db.Delete(key, nil)
fmt.Println(i)//
data, _ = db.Get(key, nil)
fmt.Println(data)//[]
//
//获取db快照
snapshot, i := db.GetSnapshot()
fmt.Println(snapshot)//leveldb.Snapshot{22}
fmt.Println(i)//
//注意: The snapshot must be released after use, by calling Release method.
//也就是说snapshot在使用之后,必须使用它的Release方法释放!
snapshot.Release()
}
levelDB使用
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
The Four
Scott Galloway / Portfolio / 2017-10-3 / USD 28.00
NEW YORK TIMES BESTSELLER USA TODAY BESTSELLER Amazon, Apple, Facebook, and Google are the four most influential companies on the planet. Just about everyone thinks they know how they got there.......一起来看看 《The Four》 这本书的介绍吧!