Chris Egidio
Jun 17 · 3 min read


This a very basic TCP server, I kept it simple for brevity. The database is a map structure, and I didn’t use concurrency locking for it. Functions — connection, reader, router and handler are modules and I use composition for them. Input or data from the client sent to the server will be in form — “COMMAND KEY VALUE\r\n”. The “\r\n” signals EOF to the server and will split into a token for the server to handle.


Function listenAndServe() creates a listener on localhost:8081, running infinitely. And when a new connection is accepted, a go-routine is fired for concurrency.


Function Reader creates a writer and reader buffer for the connection. Also, it scans the input from the connection and splits at a “\n” returning a token. The token is then passed to the router as a string.


Function router will receive the text and then map — command to handler, passing the data and writer as arguments.


A handler will compute and store data and write to the client with a response.



Build app

$ go build .

Run app

$ ./app

Now, use a telnet client to talk to the server. The server won’t close connection, so it’s open till you close.

$ telnet 8081

And, send a string to the server in a format (Command Key Value\r\n). You don’t have to use a “\r\n” at the end of the input in this case because telnet does that for you.

For example,

$ telnet 8081
Connected to
Escape character is '^]'.
SET boo foo
GET boo
SET key 10000
GET key


Software Engineering | Computer Science

Chris Egidio

Written by

Software Engineer | Marine | CS Student



Software Engineering | Computer Science

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade