This is a creation in Article, where the information may have evolved or changed.
Database functionality is often used in the application, like the key-value nature. Directly move a redis,mysql too big, fortunately there are LevelDB, directly into the application.
about what is LevelDB and LevelDB features, but Google, we look at how to use.
First to
Get Github.com/syndtr/goleveldb/leveldb
Then import in the file.
1. Open the Database
Db,err = Leveldb. OpenFile ("./db", nil)
if (Err! = nil) {Panic (err)}
If there is any mistake here, report it directly.
2. Then, read in db. Get, write in db. Put.
3. A small paragraph in the code.
Package Mainimport ("Github.com/syndtr/goleveldb/leveldb" "FMT" "StrConv")varDB *Leveldb. DBConstId_field ="ID"func init () {varErr 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)returnId}func Byte2int (Val []byte)int { varResultintresult,_= StrConv. Atoi (string(val))returnResult}func Int2byte (Valint) []byte{result:= []byte(StrConv. Itoa (val))returnresult}