Databases with database/sql
Part of the Go course
Every program you have written so far forgets everything the moment it exits. That is fine for a tool that transforms its input and stops, and useless for anything that has users, orders, sessions or history. Sooner or later a Go program needs to put data somewhere that survives it, and for most programs that somewhere is a relational database.
Go's approach to this is unusual and worth understanding before you write a line of it. The standard library ships a package called database/sql that knows nothing whatsoever about PostgreSQL, MySQL or SQLite. It defines the shape of the conversation: how you open a connection, how you send a statement, how you read rows back. The knowledge of any particular database lives in a separate package called a driver.
Want to learn Go Databases with database/sql in a real environment?
Click below, get a fresh Linux box with Go ready to go, and work through Databases with database/sql hands-on with an AI tutor that knows your code.
Start this lessonDatabases with database/sqlWhat you'll learn
- Database/sql package
- Driver imports
- Sql.Open and sql.DB pool
- Exec vs Query vs QueryRow
- Prepared statements
- Scanning rows
- Transactions
