
Hello everyone!

Iāve had to work with RocksDB recently and in order to use it from Golang I had to jump a fewhops before getting it to work properly.
This article will show you how to compile a Go program and link it with RocksDB using CGO.
We will use the grocksdb library.
What is RocksDB
RocksDB is an embedded key-value store developed by Meta.It can be used as a database engine by MySQL,MongoDB,Apache Cassandraand others.
RocksDB can be adapted for other use cases as well, such as stream processing, task queue caching, graph storage and so on.
You can view all users and use cases on their wiki page on GitHub.
Using RocksDB in a Go project on a Fedora Linux Distro
This section will show you how to use RocksDB in a Go project. We will compile RocksDB on Fedora 41.Initializing the Go project
The first step is to make your Go project and add the grocksdb requirement to the go.mod file. Mine looks like this:
Code:
module grocks-tutorial
go 1.24
require github.com/linxGnu/grocksdb v1.9.8 // indirect
Then, in your main.go file, you can add the following code:
Code:
package main
import (
"fmt"
"github.com/linxGnu/grocksdb"
"os"
)
func main() {
bbto := grocksdb.NewDefaultBlockBasedTableOptions()
bbto.SetBlockCache(grocksdb.NewLRUCache(3 << 30))
opts := grocksdb.NewDefaultOptions() // 2
opts.SetBlockBasedTableFactory(bbto)
opts.SetCreateIfMissing(true)
db, err := grocksdb.OpenDb(opts, "my_rocks") // 1
if err != nil {
fmt.Printf("err %v", err)
os.Exit(1)
}
ro := grocksdb.NewDefaultReadOptions() // 2
wo := grocksdb.NewDefaultWriteOptions() // 2
// if ro and wo are not used again, be sure to Close them.
err = db.Put(wo, []byte("foo"), []byte("bar")) // 3
value, err := db.Get(ro, []byte("foo")) // 4
fmt.Printf("The value is `%s`.\n", value.Data()) // 4
defer value.Free()
err = db.Delete(wo, []byte("foo"))
}
The code does the following things:
- Open a RocksDB database in the
my_rocks
folder. - Uses default options for opening the database, reading and writing data.
- Writes the bytes
bar
in thefoo
key. - Reads the
foo
key and prints the value.
Compiling RockDB
To compile a compatible version of RocksDB, please download version 9.8.4 from GitHub releases:The cd to the directory, install the compilers if you donāt have them and compile RocksDB as a static library.
Code:
cd rocksdb-9.8.4
sudo dnf install gcc gcc-c++
EXTRA_CFLAGS=-mpclmul EXTRA_CXXFLAGS=-mpclmul PORTABLE=x86-64-v3 USE_RTTI=1 make static_lib
The compilation will finish in a few minutes.
Installing required dependencies
Once RocksDB is compiled you can install required dependencies using the package manager:
Code:
dnf install snappy snappy-devel zlib zlib-devel bzip2 bzip2-devel lz4-devel libzstd-devel
The header files and libs will be placed in the following directories:
- /usr/include
- /usr/lib64
Compiling the Go project and linking RocksDB
To make things simpler, Iāve created a Makefile which looks like this:
Code:
run:
CGO_CFLAGS="-I/home/dnutiu/GolandProjects/grocks-tutorial/rocksdb-9.8.4/include -I/usr/include" \
CGO_LDFLAGS="-L/home/dnutiu/GolandProjects/grocks-tutorial/rocksdb-9.8.4 -L/usr/lib64 -lrocksdb -lbz2 -lstdc++ -lm -lz -lsnappy -llz4 -lzstd" \
go run .
The make file contains the path to my RocksDB include directory and lib location, as well as paths to the dependencies.
When running, the code will compile and output:
Code:
ā grocks-tutorial make run
CGO_CFLAGS="-I/home/dnutiu/GolandProjects/grocks-tutorial/rocksdb-9.8.4/include -I/usr/include" \
CGO_LDFLAGS="-L/home/dnutiu/GolandProjects/grocks-tutorial/rocksdb-9.8.4 -L/usr/lib64 -lrocksdb -lbz2 -lstdc++ -lm -lz -lsnappy -llz4 -lzstd" \
go run .
The value is `bar`.
Thatās it!
Notes on MacOS
On MacOS you can install all the packages with brew.Then execute brew info <package-name>, this will show you the location of the package, and it will contain the include and lib folders which you can then add to CGO_CFLAGS and CGO_LDFLAGS respectively.
I hope this article helped you to get started with RocksDB using Go!

You can clone the code repository from my Forge: https://forge.nuculabs.de/dnutiu/grocks-tuturial.
Update 04.04.2025 PS: Special thanks to my co-worker Cristi for helping me out figure the right environment variables for make.
Thanks for reading!