惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

博客园 - 三生石上(FineUI控件)
Hugging Face - Blog
Hugging Face - Blog
M
MIT News - Artificial intelligence
T
Tailwind CSS Blog
Webroot Blog
Webroot Blog
S
Secure Thoughts
N
News and Events Feed by Topic
月光博客
月光博客
TaoSecurity Blog
TaoSecurity Blog
Microsoft Azure Blog
Microsoft Azure Blog
B
Blog RSS Feed
N
News | PayPal Newsroom
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
小众软件
小众软件
Recent Commits to openclaw:main
Recent Commits to openclaw:main
P
Privacy & Cybersecurity Law Blog
GbyAI
GbyAI
K
Kaspersky official blog
WordPress大学
WordPress大学
P
Proofpoint News Feed
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
博客园 - 叶小钗
W
WeLiveSecurity
Jina AI
Jina AI
The Cloudflare Blog
Project Zero
Project Zero
Simon Willison's Weblog
Simon Willison's Weblog
V
Vulnerabilities – Threatpost
L
LangChain Blog
Forbes - Security
Forbes - Security
PCI Perspectives
PCI Perspectives
Engineering at Meta
Engineering at Meta
Google DeepMind News
Google DeepMind News
Recorded Future
Recorded Future
博客园 - 【当耐特】
H
Heimdal Security Blog
A
About on SuperTechFans
Cisco Talos Blog
Cisco Talos Blog
T
Threat Research - Cisco Blogs
云风的 BLOG
云风的 BLOG
Spread Privacy
Spread Privacy
L
LINUX DO - 最新话题
L
Lohrmann on Cybersecurity
Last Week in AI
Last Week in AI
Google DeepMind News
Google DeepMind News
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
I
Intezer
Martin Fowler
Martin Fowler
S
Securelist
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint

博客园 - qufo

Golang 线程池 Docker 实现的 redis 主从 Golang 之 Base62 编码 Golang 之 Qrcode 二维码 无废话 Thrift 之 Hello World( PHP 版). [转] putty 使用密钥登陆 OpenSSH 一个视图引发的血案 破一个行业ERP的感想 Practical Ext JS Projects with Gears中关于Gears描述。 七七前一天,搞定 PHP5 + Oracle 8.1.7 去掉PowerDesigner 15 在 Visual Studio 2008里的不兼容。 回家 庆祝自己通过驾驶员考试 2008.08.08.一个有记住意义的时刻。 adverbux.com 明天路考 blank.security 距离2008年8月8日还有8天。 随时10个可用线程--自己涂鸦的“线程池”
Golang 之 key-value LevelDB
qufo · 2016-07-24 · via 博客园 - qufo

时常会在应用中用到数据库功能,象 Key-Value 性质的。直接搬个 Redis,Mysql嫌大,好在有 LevelDB,直接编进应用中。

有关什么是 LevelDB 以及 LevelDB 的特性,可Google之,我们看看如何用。

先要

go get github.com/syndtr/goleveldb/leveldb

然后在文件中 import 进来。

1.打开数据库

db,err = leveldb.OpenFile("./db",nil)
if (err != nil) { panic(err) }

如果这里如果有错,直接报了。

2.然后,读用 db.Get, 写用 db.Put 。

3.代码中的一小段。

package main

import (
    "github.com/syndtr/goleveldb/leveldb"
    "fmt"
    "strconv"
)

var db *leveldb.DB

const ID_FIELD  =  "id"

func init(){
    var err error
    db,err = leveldb.OpenFile("./db",nil)
    if (err != nil) {
        panic(err)
    }

    _,err = db.Get([]byte(ID_FIELD),nil)
    if (err!=nil) {
        db.Put([]byte(ID_FIELD),[]byte("10000"),nil)
    }
}

func GetNextId() int {
    ids,err := db.Get([]byte(ID_FIELD),nil)
    if (err != nil) {
        fmt.Println(err)
    }
    id := Byte2int(ids)
    db.Put([]byte(ID_FIELD),Int2byte(id+1),nil)
    return id
}

func Byte2int(val []byte) int {
    var result int
    result,_ = strconv.Atoi(string(val))
    return result
}

func Int2byte(val int) []byte {
    result := []byte(strconv.Itoa(val))
    return result
}