Speaking in Frames
RESP: the first byte tells you the type, lengths are declared up front.
What you're wiring up
RESP is Redis's wire format, and it's beautifully learnable: the first byte gives the type (+ simple string, - error, : integer, $ bulk string, * array), and lengths are declared before payloads so you never guess where a message ends. Every client command is an array of bulk strings.
This stage replaces Stage 1's "contains PING" hack with an honest parser and serializer — the same "schema on the wire" idea DDIA's encoding chapter is about. The parser must survive the stream reality: a frame may arrive split across two reads, or ten frames may arrive in one.
# SET name ansh, on the wire: *3\r\n$3\r\nSET\r\n$4\r\nname\r\n$4\r\nansh\r\n
Assembly steps
hint
bufio.Reader.ReadString('\n') handles the partial-read problem for you — it blocks until the delimiter arrives. Strip the \r.
hint
After a $N length line, read exactly N bytes then consume the trailing \r\n — bulk strings may legally contain \r\n inside.
hint
You'll need the null bulk constantly from Stage 3 on.
hint
strings.ToUpper(args[0]) in a switch is enough — resist building a command-registry framework.
hint
Only tear down the connection on a malformed frame (unparseable bytes), not on an unknown command.
Go deeper (after it passes)
Pairs with DDIA M4 — Wire Formats That Survive Change, and Clean Code M5 — Errors Without the Clutter.