Golang project

Build a custom key-value store using Go

Yassine Hoummani
4 min readJan 16, 2021
golang project — build a custom key-value store

What is a key-value store?

A key-value store or a key-value database is a data storage paradigm designed for storing, retrieving, and managing associative arrays, and data structures more commonly known today as dictionary or hash tables. Dictionaries contain a collection of objects, or records, which in turn have many different fields within them, each containing data. These records are stored and retrieved using a key that uniquely identifies the record and is used to find the data within the database.

There are several popular use cases for key-value databases:

  • Web applications may store user data
  • Real-time operations
  • In-memory caching to speed up applications by minimizing reads and writes

Implemented features

The presented program will implement the four fundamental tasks of the key-value store:

  • Adding a new element
  • Deleting an existing element from the store based on a key or id
  • Looking up the value of a specific key in the store
  • Changing the value of an existing key

In addition to that, the program will be executed based on commands for example the program will stop when you enter STOP word as input and will print out to the console the full content of the key-value store when you enter the PRINT command.

Definition of our custom type

In Go language, you can use a struct to define your custom type, in our case the custom type is a book that contains ID, Name, and Author, all the fields are a type of string.

The key-value store is stored in a native Go map because using a built-in Go structure is usually faster.

The map variable is defined as a global variable, where its keys are string variables and its values are book variables.

Build the core functionalities

The code below contains the most used functions in the key-value store which known as CRUD operations(Create, Read, Update, and Delete), respectively add, loockUp, change, deleteElement.

Note that if the user tries to add a new element to the store without given enough values to populate the book struct, the add function will fail. For this particular program, the missing fields of the book struct will be set to the empty string. However, if you try to add a key that already exists, you will get an error message instead of modifying the value of the existing key.

Also, if you try to change a key that does not exist, the program will add that key to the store without generating any error message.

Main program

The main program begins with reading the input(commands and arguments) from the user.

Firstly, the for loop makes sure that the program will keep running for as long as the user provides some input. Secondly, the program makes sure that args slice has at least five-element. Thus, for an ADD operation to be complete and have any missing values, you will need input like that: ADD akey Field1 field2 field3.

The program needs to process the input from the user to know which functions should be executed and which arguments should be present based on the command name.

Executing the solution

Here some demonstration of the proposed solution, you can try it by yourself and if you have any suggestion ideas or any question about this please let me know.

Add new element

To avoid any error in the executing phase, don’t use the space character into the name of the book and author-name, so, the command should be like so:

ADD book1 123 The_Lord_of_Rings Tolkien

key-value store using Golang Add new element

Print the content of the store

key-value store in golang print out the full content of store

Change an element

key-value store project in golang update element of store

Delete an element

key-value store project using Go Language Delete an element of store

Stop the program

key-value store project using Golang stop the main program

Full process

key-value store project using golangmain program

Summary

A key-value store is a useful paradigm for handling mixed data that can be used for automation, caching, etc. This article implemented the most frequent operations of a key-value database in memory using the best language for systems programming(Golang). Feel free to ask your questions if you have any misunderstanding to get prepare for the next projects.

--

--