LevelDBGoWrapper is a Go wrapper for LevelDB. It also demonstrates a way to bind C++ codes at Golang programs, which is leverage on cgo and swig.
-
Install leveldb:
leveldb is a submodule as this repo. You are able to install leveldb by following commands:
> git submodule update --init > cd leveldb > make
-
Install swig:
swig is able to easily be installed by apt-get or brew install.
-
How to bind leveldb to LevelDBGoWrapper
-
Firstly install leveldb which will generate include files and shared-lib. Put these to
/usr/local/include
and/usr/local/lib
to make gcc/g++ to be able to find leveldb library easier. -
Write wrapper file like
leveldb_interface.*
is c++ wrapper function which is going to expose to golang function. Swig compiles our c++ programs with leveldb library and generate its binding go-codeleveldbwrapperlib.go
andleveldbwrapper_wrap.cxx
. -
lib.go
is to indicate package name of wrapper. -
leveldbwrapperlib.i
is swig file to indicate what functions is it going to expose to golang. -
To generate binding codes, following command
swig -go -cgo -c++ -intgosize 64 leveldbwrapperlib.i
-
Make our wrapper link leveldb at runtime. Add a line
#cgo LDFLAGS: -lleveldb
just right ahead ofimport "C"
-
-
Test and Benchmark
-
simple test:
run
go run cmd/main.go
, it works when it showselsa
-