stmt, err := tx.Prepare("INSERT INTO users (name, email) VALUES (?, ?)") if err != nil { return err } defer stmt.Close() _, err = stmt.Exec("John Doe", "[email protected]") if err != nil { return err } err = tx.Commit() if err != nil { return err }
stmt, err := tx.Prepare("UPDATE customers SET balance = balance + ? WHERE id = ?") if err != nil { return err } defer stmt.Close() _, err = stmt.Exec(100, 123) if err != nil { return err } err = tx.Commit() if err != nil { return err }This example prepares a statement for updating a customer's balance in a database, executes it within a transaction, and commits changes. The "database/sql" package library provides a flexible and powerful way to work with SQL databases in Go, allowing developers to easily execute transactions, prepare statements, and manage connections.