Go/
Lesson

Your AI agent just suggested installing 12 dependencies to build a simple APIWhat is api?A set of rules that lets one program talk to another, usually over the internet, by sending requests and getting responses.. Stop. Before you let it npm-install its way into dependencyWhat is dependency?A piece of code written by someone else that your project needs to work. Think of it as a building block you import instead of writing yourself. hell, understand what Go code AI actually generates well, and where it hallucinates.

The decision: framework or stdlib?

You're building a new APIWhat is api?A set of rules that lets one program talk to another, usually over the internet, by sending requests and getting responses. endpointWhat is endpoint?A specific URL path on a server that handles a particular type of request, like GET /api/users.. AI confidently suggests: "Let's use Gin! It's fast and has great middlewareWhat is middleware?A function that runs between receiving a request and sending a response. It can check authentication, log data, or modify the request before your main code sees it.!" It generates 40 lines of boilerplateWhat is boilerplate?Repetitive, standardized code that follows a known pattern and appears in nearly every project - like setting up a server or wiring up database connections., adds three imports you've never heard of, and creates a router setup that looks nothing like the docs.

Here's what AI won't tell you: Go's standard libraryWhat is standard library?A collection of ready-made tools that come built into a language - no install required. Covers common tasks like reading files or making web requests. has had a perfectly good HTTPWhat is http?The protocol browsers and servers use to exchange web pages, API data, and other resources, defining how requests and responses are formatted. router since 2009. And since Go 1.22, it's actually great.

The problem isn't that frameworks are bad. It's that they add cognitive load AI doesn't need. Every framework has its own patterns, its own magic, its own way of doing things. When AI generates framework code, it has to remember conventions correctly. Sometimes it does. Sometimes it invents APIs that don't exist.

The standard library is consistent, well-documented, and baked into every LLM's training data. When you ask for "a Go HTTP server with standard library," AI knows exactly what to produce.

AI pitfall
AI will sometimes generate Gin code that mixes gin.Context methods with standard http.ResponseWriter methods. The result compiles but behaves incorrectly, Gin's context wraps the response writer, so writing to both causes duplicate headers or garbled responses. If you didn't choose Gin deliberately, switch to stdlib.
02

When to use the standard libraryWhat is standard library?A collection of ready-made tools that come built into a language - no install required. Covers common tasks like reading files or making web requests. (most of the time)

Native HTTPWhat is http?The protocol browsers and servers use to exchange web pages, API data, and other resources, defining how requests and responses are formatted. routing since Go 1.22

Before Go 1.22, you needed gorilla/mux or chi for path parameters. Not anymore.

When you ask AI to build an APIWhat is api?A set of rules that lets one program talk to another, usually over the internet, by sending requests and getting responses., it should generate something like:

mux := http.NewServeMux()

// Method-specific routes (Go 1.22+)
mux.HandleFunc("GET /users", listUsers)
mux.HandleFunc("POST /users", createUser)
mux.HandleFunc("GET /users/{id}", getUser)
mux.HandleFunc("DELETE /users/{id}", deleteUser)

func getUser(w http.ResponseWriter, r *http.Request) {
    id := r.PathValue("id")  // Path parameter extraction
    fmt.Fprintf(w, "User ID: %s", id)
}

No framework. No dependencies. AI generates this correctly almost every time.

JSONWhat is json?A text format for exchanging data between systems. It uses key-value pairs and arrays, and every programming language can read and write it. handling

The encoding/json package is one of AI's strongest areas. Struct tags, marshaling, unmarshaling, AI gets this right consistently.

type User struct {
    ID    int    `json:"id"`
    Name  string `json:"name"`
    Email string `json:"email,omitempty"`
}

user := User{ID: 1, Name: "Alice"}
data, _ := json.Marshal(user)

Decision table: stdlib vs external

What you needUse stdlib?If not, use
HTTP routingYes (Go 1.22+)Chi if pre-1.22
Path parametersYes (r.PathValue),
JSON encode/decodeYes (encoding/json),
Middleware chainsManual wrappers workChi for complex chains
Input validationNogo-playground/validator
WebSocketsNogorilla/websocket
Database ORMdatabase/sql for simple queriesGORM or sqlc for complex
Structured loggingNo (stdlib log is basic)slog (Go 1.21+) or zerolog
03

When you actually need a library

Database: GORM vs sqlc

GORM is an ORMWhat is orm?Object-Relational Mapping - a library that lets you interact with a database using your programming language's objects instead of writing raw SQL. that AI handles well for basic operations, struct tags and auto-migrations are patterns AI knows.

type User struct {
    gorm.Model
    Name  string `gorm:"uniqueIndex"`
    Email string
}

db.AutoMigrate(&User{})
db.Create(&User{Name: "Alice", Email: "alice@example.com"})

But GORM associations (HasMany, BelongsTo) are where AI hallucinates. It'll generate Preload calls with incorrect syntax or suggest relationships that don't match your schemaWhat is schema?A formal definition of the structure your data must follow - which fields exist, what types they have, and which are required..

For complex queries, consider sqlc, you write SQLWhat is sql?A language for querying and managing data in relational databases, letting you insert, read, update, and delete rows across tables., it generates type-safe Go code. Less room for AI error.

AI pitfall
AI loves suggesting db.AutoMigrate everywhere, including production startup code. AutoMigrate modifies your schema on every deploy, it can add columns but never removes them, and it doesn't handle data migrations. Use it in development only. Production needs versioned migration files.
04

Frameworks to avoid with AI

FrameworkWhy AI struggles with itBetter alternative
Gin*gin.Context magic, AI mixes stdlib and Gin patternsChi or stdlib
BuffaloVery opinionated, low training data representationstdlib + specific libs
BeegoLess English documentation, less training datastdlib or Chi
RevelHeavy code generation, complex conventionsstdlib

These frameworks aren't bad. They're just harder for AI to get right consistently. As training data improves, this will change.

05

Prompting AI for reliable Go code

The way you prompt matters. Compare:

Vague (hallucinationWhat is hallucination?When an AI generates confident but false information - fabricated facts, invented citations, or non-existent code methods.-prone):

"Create a Go web server with user authentication"

Result: AI imports random auth libraries, suggests complex JWTWhat is jwt?JSON Web Token - a self-contained, signed token that carries user data (like user ID and role). The server can verify it without a database lookup. packages, or uses Gin.

Specific (reliable):

"Create a Go HTTP server using only net/http (Go 1.22+).
Use basic auth with hardcoded credentials for now.
No external dependencies."

Result: Clean, predictable stdlib code.

What you wantPrompt that works
REST API"REST API using net/http Go 1.22+ with GET /users/{id} and POST /users"
Database"GORM with SQLite, User model with name and email, no associations"
JSON API"JSON API handler using encoding/json, parse request body, return structured response"
Middleware"HTTP middleware for logging request duration using standard library"
06

Key takeaway

Before your AI agent imports a framework, ask: "Could this be done with the standard libraryWhat is standard library?A collection of ready-made tools that come built into a language - no install required. Covers common tasks like reading files or making web requests.?" The answer is almost always yes. Stdlib code is more reliable from AI, easier to debug, and deploys without dependencyWhat is dependency?A piece of code written by someone else that your project needs to work. Think of it as a building block you import instead of writing yourself. headaches.