bolt 使用示例
🕠 Tue, 02 Feb 2016 by GolangNote
bolt 是一款高性能的key value 数据库,下面是它的使用示例:
package main
import (
"encoding/binary"
"encoding/json"
"fmt"
"log"
"time"
"github.com/boltdb/bolt"
)
type Post struct {
Created time.Time
Title string
Content string
}
type User struct {
ID int
Name string
}
func main() {
db, err := bolt.Open("my.db", 0600, &bolt.Options{Timeout: 1 * time.Second})
if err != nil {
log.Fatal(err)
}
defer db.Close()
/*
db.Update(func(tx *bolt.Tx) error {
b, err := tx.CreateBucketIfNotExists([]byte("posts"))
if err != nil {
return err
}
return b.Put([]byte("2015-01-01"), []byte("My New Year post"))
})
*/
db.View(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte("posts"))
v := b.Get([]byte("2015-01-01"))
fmt.Printf("%s\n", v)
if err := b.ForEach(func(k, v []byte) error {
fmt.Printf("A %s is %s.\n", k, v)
return nil
}); err != nil {
return err
}
return nil
})
post := &Post{
Created: time.Now(),
Title: "My first post",
Content: "Hello, this is my first post.",
}
db.Update(func(tx *bolt.Tx) error {
b, err := tx.CreateBucketIfNotExists([]byte("posts"))
if err != nil {
return err
}
encoded, err := json.Marshal(post)
if err != nil {
return err
}
return b.Put([]byte(post.Created.Format(time.RFC3339)), encoded)
})
db.Update(func(tx *bolt.Tx) error {
b, err := tx.CreateBucketIfNotExists([]byte("posts"))
if err != nil {
return err
}
encoded, err := json.Marshal(post)
if err != nil {
return err
}
return b.Put([]byte(post.Created.Format(time.RFC3339)), encoded)
})
db.Update(func(tx *bolt.Tx) error {
b, err := tx.CreateBucketIfNotExists([]byte("MyBucket"))
err = b.Put([]byte("answer"), []byte("42"))
return err
})
db.View(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte("MyBucket"))
v := b.Get([]byte("answer"))
fmt.Printf("%s\n", v)
return nil
})
db.Update(func(tx *bolt.Tx) error {
u := User{}
// Retrieve the users bucket.
// This should be created when the DB is first opened.
//b := tx.Bucket([]byte("users"))
b, err := tx.CreateBucketIfNotExists([]byte("users"))
// Generate ID for the user.
// This returns an error only if the Tx is closed or not writeable.
// That can't happen in an Update() call so I ignore the error check.
id, _ := b.NextSequence()
u.ID = int(id)
// Marshal user data into bytes.
buf, err := json.Marshal(u)
if err != nil {
return err
}
// Persist bytes to users bucket.
return b.Put(itob(u.ID), buf)
})
// Delete the key in a different write transaction.
if err := db.Update(func(tx *bolt.Tx) error {
return tx.Bucket([]byte("MyBucket")).Delete([]byte("answer"))
}); err != nil {
log.Fatal(err)
}
db.View(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte("users"))
if err := b.ForEach(func(k, v []byte) error {
fmt.Printf("A %s is %s.\n", k, v)
return nil
}); err != nil {
return err
}
return nil
})
}
// itob returns an 8-byte big endian representation of v.
func itob(v int) []byte {
b := make([]byte, 8)
binary.BigEndian.PutUint64(b, uint64(v))
return b
}
官方网站 https://github.com/boltdb/bolt 56
官方示例 https://godoc.org/github.com/boltdb/bolt#pkg-examples 54
本文网址: https://golangnote.com/topic/109.html (转载注明出处)
关于GolangNote:记录在工作中使用golang 遇到、面临的相关问题及解决方法。如果你在这里获得一些知识或信息,解决你的编程问题,请考虑捐赠给不幸的人或者你喜欢的慈善机构,除捐赠外,种植树木、志愿服务或减少排碳的行为也很有益处。如果你有任何问题可以在下面 留言
Be the first to comment!
Relative Articles
Recent Go Articles
- Golang 把cookie 字符串解析为cookie 结构
- Golang 计算字符串中包含某个或某些字符集的个数
- 使用Golang 对文件增删写读操作备忘
- Go Modules 使用备忘
- 使用Golang 简单删除图片exif 信息
- 谷歌翻译的 golang 库推荐
- Go 1.13.2 与1.13.3 紧急更新
- golang 人脸检测识别库
- Go build 错误 “stackcheck redeclared in this block previous declaration”的解决方法
- Golang phantomjs 动态代理实现
- Golang chrome debug protocol 库推荐
- Golang 随机打乱数组/Slice
- Golang sync.WaitGroup 的 Wait 超时处理
- Golang实现简单的Socks5代理
- Golang 用snappy + Base64 简单压缩加密进行网络传输
- Golang http IPv4/IPv6 服务
- golang 全角半角相互转换
- 在自己的网站部署TLS 1.3
- Golang 实现/打印菜单树
- Golang telegram 机器人小试
Top Go Articles
- golang 当前时间、时间戳、时区的相互转化
- golang map 按value 大小排序,降序和升序
- golang regexp 正则表达式常见的用法
- golang 正确获取绝对路径的方法
- Golang 定时循环的实现
- golang 把上传文件转为byte
- Golang 时区时差处理方式
- golang 用正则包regexp 通过user-agent 识别手机浏览器
- GoLang 正则判断字符是不是中文
- golang 生成良好的唯一ID/uuid库比较
- Golang 把cookie 字符串解析为cookie 结构
- Golang 获取两个数之间的随机数
- golang 用gzip 压缩、解压缩字符串
- GoLang 计算小文件和大文件 md5 值的例子
- Golang Web 自动安装、更新Let’s Encrypt 免费证书
- go 获取硬盘的可用空间的方法
- golang 人脸检测识别库
- Golang 生成防识别的图片验证码
- Go build 错误 “stackcheck redeclared in this block previous declaration”的解决方法
- GoLang 实现python timedelta 函数功能