ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
# Databases One of the most asked questions I get about web development in Go is how toconnect to a SQL database. Thankfully, Go has a fantastic SQL package in thestandard library that allows us to use a whole slew of drivers for differentSQL databases. In this example we will connect to a SQLite database, but thesyntax (minus some small SQL semantics) is the same for a MySQL or PostgreSQLdatabase. ~~~ package main import ( "database/sql" "fmt" "log" "net/http" _ "github.com/mattn/go-sqlite3" ) func main() { db := NewDB() log.Println("Listening on :8080") http.ListenAndServe(":8080", ShowBooks(db)) } func ShowBooks(db *sql.DB) http.Handler { return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) { var title, author string err := db.QueryRow("select title, author from books").Scan(&title, &author) if err != nil { panic(err) } fmt.Fprintf(rw, "The first book is '%s' by '%s'", title, author) }) } func NewDB() *sql.DB { db, err := sql.Open("sqlite3", "example.sqlite") if err != nil { panic(err) } _, err = db.Exec("create table if not exists books(title text, author text)") if err != nil { panic(err) } return db } ~~~ ## Exercises 1. Make use of the `Query` function on our `sql.DB` instance to extract a collection of rows and map them to structs. 1. Add the ability to insert new records into our database by using an HTML form. 1. `go get github.com/jmoiron/sqlx` and observe the improvements made over the existing database/sql package in the standard library.