shad-go/lectures/07-sql/tx/tx.go
2021-04-08 14:39:10 +03:00

24 lines
378 B
Go

package tx
import (
"context"
"database/sql"
"log"
)
func Begin(ctx context.Context, db *sql.DB) {
tx, err := db.BeginTx(ctx, nil)
if err != nil {
log.Fatal(err)
}
defer tx.Rollback()
_, err = tx.ExecContext(ctx, `UPDATE users SET name = "Tyador Borlú" WHERE id = 1`)
if err != nil {
log.Fatal(err)
}
if err = tx.Commit(); err != nil {
log.Fatal(err)
}
}