28.8 sql.NullString and Handling Database NULLs

Right, let’s talk about one of the most reliably annoying little handshakes between Go’s type system and the messy reality of your database: NULL. Go has a wonderful, strict, “zero-value” philosophy. A string is never nil; it’s just an empty string "". The database world, however, is a land of ambiguity and forgotten data. A VARCHAR column can absolutely be NULL, meaning “this value is intentionally unknown.” It’s not empty, it’s not zero, it’s nothing. And Go despises this kind of ambiguity.

28.7 Connection Pool Tuning: MaxOpenConns, MaxIdleConns, ConnMaxLifetime

Right, let’s talk about connection pools. You’ve probably already used sql.Open() and thought, “Great, I have a database handle.” What you actually have is a handle to a pool of connections, managed by the database/sql package. This is fantastic—it saves you from the monumental pain of managing connections yourself. But like any powerful tool, it has knobs. And if you ignore these knobs, your application will eventually, and spectacularly, fall over in production at 3 AM. I’ve been there. Let’s not do that.

28.6 Transactions: Begin, Commit, Rollback, and Deferred Rollback

Right, let’s talk transactions. This is where we stop playing in the sandbox and start building castles, with all the attendant risk of them collapsing into a pile of mud. A transaction bundles multiple SQL operations into a single, all-or-nothing unit of work. It’s the database’s way of saying, “I promise I will do all of this, or absolutely none of it. There is no in-between.” Think of it like ordering a burger and fries. You don’t want a system where the kitchen charges you for the burger, makes it, but then burns the fries and just… doesn’t give you any. That’s a failed state. The transaction is you placing the entire order. The restaurant either commits to making both items successfully, or they roll the whole thing back and you get nothing (and hopefully your money back). The atomicity—the “all-or-nothing” property—is the entire point.

28.5 Prepared Statements: Performance and SQL Injection Prevention

Right, let’s talk about prepared statements. This is one of those topics where if you’re not using them, you’re not just leaving performance on the table—you’re actively leaving the back door to your database wide open. I’m going to show you how to use them properly in Go’s database/sql package so you can stop doing that. The core idea is simple: instead of concatenating user input into your SQL string like it’s 1999, you use a placeholder (? or $1 depending on your database) in your query template. You then provide the actual values separately. The database driver handles the rest. This gives you two monumental advantages:

28.4 Scanning Rows into Go Values

Right, let’s get to the part where you actually get your data out of those query results and into something useful in your Go program. This is where most people’s first encounters with database/sql go from “oh, this is easy” to a guttural “oh, come on.” I’m here to save you the headache. You’ve executed your Query or QueryContext and you’re holding a *sql.Rows object. Think of this Rows object as a slightly rude museum attendant pointing a flashlight at one row of data at a time. It’s your job to look at the current row, scribble down what you see (Scan it), and then tell the attendant to move to the next one. You do this in a loop until they tell you there’s nothing left to see.

28.3 Query, QueryRow, and Exec: Running SQL

Alright, let’s get our hands dirty with the three workhorses of the database/sql package: Query, QueryRow, and Exec. These are the functions you’ll use 99% of the time to actually talk to your database. They might look similar at a glance, but they serve distinct purposes and, more importantly, they have wildly different failure modes. Using the wrong one is like using a screwdriver to hammer a nail—it might eventually work, but you’re going to have a bad time and probably damage something.

28.2 sql.DB vs sql.Conn: The Pool and a Single Connection

Right, let’s settle this. You’ve got your sql.DB object, and you’ve probably seen sql.Conn lurking in the docs. You might be thinking, “Why do I need two different things to talk to the database? Isn’t one enough?” The designers, in their infinite wisdom, decided no. And for once, I’m with them. It’s not just redundancy; these two serve fundamentally different masters. Think of sql.DB not as a single database connection, but as the manager of a whole pool of them. It’s your highly efficient, slightly cynical office manager. You, the developer, hand a task (a query) to the manager (sql.DB), and it figures out the best idle worker (a connection) in the pool to handle it. When the worker is done, it goes back to the pool to wait for the next job. This is phenomenally efficient because creating a new TCP connection and database session from scratch is brutally expensive. The pool avoids that overhead.

28.1 Registering a Driver and Opening a Connection

Right, let’s get our hands dirty. Before you can ask the database anything, you need two things: a driver and a connection. It sounds simple, and it is, but the Go way of doing it is a little… unique. Let’s just say the designers had a very strong opinion about avoiding magic, and they stuck to it. First, the driver. Think of it as the translator for a specific database dialect (PostgreSQL, MySQL, etc.). The database/sql package is the generic, all-powerful boss who only speaks in abstract concepts like “connections” and “results.” The driver is the poor soul who has to actually implement those concepts for a specific database.

— joke —

...