Go REST API bugs junior developers fix first
A Go API can compile, pass a quick manual test and still break its contract. A list endpoint ignores the limit you sent, or a token from yesterday still gets in. This guide covers the bugs that come up most in a Go net/http service, how to spot each one, and how to test the fix. The examples come from Servly, Kadra's Go REST API practice codebase.
Apply limit and offset, with a default and a cap
A list endpoint that ignores pagination returns every row on every request. It works with ten rows in development and times out with a million in production. That's Servly's API-301 ticket.
A fix has to handle the parameters a client forgot or got wrong, not just the happy path. Servly uses a default of 20 and a cap of 100:
limit, err := strconv.Atoi(r.URL.Query().Get("limit"))
if err != nil || limit <= 0 || limit > MaxLimit {
limit = DefaultLimit
}
offset, err := strconv.Atoi(r.URL.Query().Get("offset"))
if err != nil || offset < 0 {
offset = 0
}
if offset >= len(records) {
return []Record{}
}
end := min(offset+limit, len(records))
return records[offset:end]Test the edges: no limit at all, a limit over the cap, an offset past the end, and two pages in a row that don't overlap.
Check the expiry, not just the signature
A valid signature proves the server issued a token. It says nothing about whether the token is still good. In API-315 the middleware checks the signature and skips the exp claim, so a token that expired hours ago still authenticates.
The check itself is one line. The part people get wrong is the boundary: a token that expires exactly now is expired.
now := clock.Now()
if !now.Before(tok.ExpiresAt) {
return Token{}, ErrTokenExpired // the handler turns this into a 401
}Notice the clock. Servly reads time through a Clock interface instead of calling time.Now() directly, so a test can set the time to one second before and one second after expiry without sleeping.
Rate limit per API key, not per process
A rate limiter with one shared counter lets a single noisy client use up the limit for everybody. API-322, the senior ticket in Servly, asks for a separate bucket for each API key, each with its own reset window.
func (rl *RateLimiter) Allow(key string) bool {
rl.mu.Lock()
defer rl.mu.Unlock()
now := rl.clock.Now()
b, ok := rl.buckets[key]
if !ok || !now.Before(b.resetAt) {
b = &bucket{resetAt: now.Add(rl.window)}
rl.buckets[key] = b
}
if b.count >= rl.limit {
return false
}
b.count++
return true
}The mutex matters because handlers run on many goroutines at once. Run your tests with go test -race to catch a missing lock. In production you'd also clear out old buckets so the map doesn't grow forever, and if the service runs on several machines, the counters have to live in a shared store such as Redis.
When a client is over the limit, return 429 Too Many Requests with a Retry-After header, so a well-behaved client knows when to try again.
Practice on a Go service
Servly is a Go net/http service with the API tickets above planted in it. You get your own GitHub repo, fix each ticket on a branch and open a pull request. An AI teammate reviews the diff, and a hidden go test suite decides whether the fix passes.
Every Servly repo opens in GitHub Codespaces with Go 1.25 installed, so there's nothing to set up on your own machine.
Questions people ask
Should I use offset or cursor pagination?
Offset pagination is simple and fine for small or slow-changing tables. For large tables, or ones that get new rows while people page through them, a cursor based on a sorted key avoids skipped and repeated rows.
Should an expired token return 401 or 403?
401. The request didn't prove who it's from, so the client should get a new token and retry. 403 means the server knows who you are and you still aren't allowed.
How do I test time-based code in Go without sleeping?
Pass the time in. Define a small Clock interface with a Now method, use the real clock in production and a fake one in tests that you can move forward by hand.
Is Kadra free?
Yes, Kadra is free during beta with no credit card. After beta, Pro is planned at $12 a month or $79 a year, and the demo and your first full sprint stay free.
Try it on a real ticket
Pick a stack, fix a bug in a working codebase and get your pull request reviewed. Free during beta.
More guides
- How to get software engineering experience before your first job
- What your first code review at a software job looks like
- How to fix a bug in a codebase you didn't write
- What is a software engineering job simulator?
- Money bugs junior Java developers fix in banking code
- Common bugs in a Node.js CMS backend, and how to fix them
- Python ETL bugs junior data engineers fix first
- Payment bugs Python developers fix in a FastAPI service
- Booking bugs C# developers fix in ASP.NET Core
- Checkout bugs developers fix in a Next.js store
- Realtime chat bugs developers fix in TypeScript