This is a creation in Article, where the information may have evolved or changed.
Leveldb is a very powerful KV database, naturally, I also want to be able to use in go.
If there is an official go leveldb implementation, then I would prefer, such as this, but the library document is not at all, and no one on the internet to use the actual combat environment, whether it can be used in the production environment using the question mark, insurance, I decided not to use.
Because LEVELDB has C interface, so through the CGO can be very convenient integration, so I decided to adopt this way, fortunately, someone has done a cgo version, that is, Levigo.
Use Levigo, need to compile the installation leveldb, if need compression support also need to compile snappy, in order to be simple, I wrote a widget script, as follows:
#!/bin/bash#refer https://github.com/norton/lets/blob/master/c_src/build_deps.sh# You must set up the actual snappy and LEVELDB source address here snappy_src=./snappyleveldb_src=./leveldbsnappy_dir=/usr/local/snappyleveldb_dir= /USR/LOCAL/LEVELDBIF [!-f $SNAPPY _dir/lib/libsnappy.a]; Then (CDs $SNAPPY _SRC && \/configure--prefix= $SNAPPY _dir && \ Make && \ Make install) Else echo "Skip install snappy" Fiif [!-f $LEVELDB _DIR/LIB/LIBLEVELDB.A]; Then (CD $LEVELDB _src && \ echo "echo \" Platform_cflags+=-i$snappy_dir/include\ ">> build_config.m K ">> Build_detect_platform && echo" echo \ "Platform_cxxflags+=-i$snappy_dir/include\" >> Build _config.mk ">> build_detect_platform && echo" echo \ "Platform_ldflags+=-l $SNAPPY _dir/lib-lsnappy\" >> build_config.mk ">> build_detect_platform && make Snappy=1 && \ Make &&am P \ mkdir-p $LEVELDB _dir/include/leveldb && \ Install include/leveldb/*.h $LEVELDB _dir/include/leveldb && \ mkdir-p $LEVELDB _dir/l IB && \ Cp-af libleveldb.* $LEVELDB _dir/lib) Else echo "Skip install LEVELDB" Fifunction Add_path () {# $ 1 Path variable # $ path to add if [-D "$"] && [[": $:"! = * ": $:" *]]; Then echo "$1:$2" Else echo "$" fi}export cgo_cflags= "-i$leveldb_dir/include-i$snappy_dir/include" Export cgo_ldf lags= "-l$leveldb_dir/lib-l$snappy_dir/lib-lsnappy" Export ld_library_path=$ (add_path $LD _library_path $SNAPPY _dir/ LIB) Export ld_library_path=$ (Add_path $LD _library_path $LEVELDB _dir/lib) go get Github.com/jmhodges/levigo
For the use of leveldb in Go, Levigo did a good package, but one thing I'm not used to, in Leveldb, for read and write operation, all need to pass in an option, this thing is a default option object most of the time, There's no need to create a delete every time you go inside. So I encapsulated it, provided the following interface, so that the default option is used.
For iterator, I refer to the C + + model, which provides the iterator and reverse_iterator two modes, as follows:
The specific code is here.