db, err := sql.Open("mysql", "user:password@tcp(localhost:3306)/mydatabase") if err != nil { log.Fatal(err) } tx, err := db.Begin() if err != nil { log.Fatal(err) } _, err = tx.Exec("INSERT INTO mytable (name, age) VALUES (?, ?)", "John Smith", 25) if err != nil { tx.Rollback() log.Fatal(err) } _, err = tx.Exec("INSERT INTO mytable (name, age) VALUES (?, ?)", "Jane Doe", 30) if err != nil { tx.Rollback() log.Fatal(err) } err = tx.Commit() if err != nil { tx.Rollback() log.Fatal(err) }In this example, we start a new transaction using the db.Begin() method. We then perform two database inserts within the transaction. If either of these inserts fails, we call tx.Rollback() to undo any changes that have been made. If both inserts are successful, we call tx.Commit() to finalize the transaction. The database/sql package is a standard package library included with the Go language.