textfilekv is a pure Go based key-value store on plain text file as =
delimited lines of text.
Each line represent a new record. This file can be even edited by hand when not in use to edit/update.
To start using textfilekv, install Go and run go install
:
$ go install github.com/miteshbsjat/textfilekv@latest
It is just required to provide path of text file to store the key-value(s). This file will be created if not present.
test_kv.go
package main
import (
"fmt"
tkv "github.com/miteshbsjat/textfilekv"
)
func main() {
filePath := "data.txt"
kvs, err := tkv.NewKeyValueStore(filePath)
if err != nil {
fmt.Printf("Error in creating/reading %s for textfilekv %v\n",
filePath, err)
}
kvs.Set("name", "John")
kvs.Set("age", "30")
kvs.Set("name", "Mitesh")
kvs.Set("age", "41")
kvs.Set("data", "{\"height\": 167}")
name, exists := kvs.Get("name")
if exists {
fmt.Printf("Name: %s\n", name)
}
age, exists := kvs.Get("age")
if exists {
fmt.Printf("Age: %s\n", age)
}
data, exists := kvs.Get("data")
if exists {
fmt.Printf("Data: %s\n", data)
}
demokey, exists := kvs.Get("demokey")
if exists {
fmt.Printf("Data: %s\n", demokey)
}
}
Run
go run test_kv.go
Output
Name: Mitesh
Age: 41
Data: {"height": 167}
Now adding the key demokey
by running following command after first run of this program.
echo 'demokey=demovalue' >> /tmp/data.txt
Output
Name: Mitesh
Age: 41
Data: {"height": 167}
Demokey: demovalue
Testing can be done by running go test
go test
textfilekv
has the following features for now.
- Simple to comprehend and update using *nix editors like. nano, vim, emacs, even filter programs
- Same format as
.env
file supported by config tools or libraries likeviper
- Useful for programs which need only limited set of key-values stored in a file
This is quite simple text file based key value store written in go, there are quite a few areas for improvements and optimizations.