Junior — Middle
Kuidas rakendada tehingute haldust süsteemis?
sobes.tech AI
Vastus AI-lt
In Go, transaction management is typically implemented using the Begin(), Commit() and Rollback() methods from the database/sql package. First, a transaction is created via db.Begin(), then the necessary operations are performed, and finally, the transaction is either committed (Commit()) or rolled back (Rollback()) in case of an error.
Example:
tx, err := db.Begin()
if err != nil {
// error handling
}
_, err = tx.Exec("INSERT INTO users(name) VALUES(?)", "Alice")
if err != nil {
tx.Rollback()
// error handling
}
err = tx.Commit()
if err != nil {
// error handling
}
This approach guarantees the atomicity of operations and allows control over data integrity.