goback

package module
v0.0.0-...-a49ca3c Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 5, 2015 License: MIT Imports: 0 Imported by: 2

README

goback

A simple non-database related transaction library

Typical use case

A function with a sequence of steps and if any of the steps fail all previous steps need to be reverted (e.g., variable values restored, file(s) deleted, etc.)

Example

func Bar(i *int) {
	tx := goback.Begin()
	defer tx.Rollback()

	old := *i
	tx.Exec(func() error {*i = old; return nil})
	*i = 42

	err := fmt.Errorf("Forced error")
	
	if err == nil {
		tx.Commit()
	}
}

func main() {
	i := 10
	Bar(&i)
	fmt.Println(i)
}

Prints...

10

...because the tx.Commit() line never executes.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Tx

type Tx struct {
	// contains filtered or unexported fields
}

func Begin

func Begin() *Tx

func (*Tx) Commit

func (t *Tx) Commit()

func (*Tx) Exec

func (t *Tx) Exec(f TxFunc)

func (*Tx) Rollback

func (t *Tx) Rollback() error

Rollback all acctions by calling all rollback functions.

type TxFunc

type TxFunc func() error

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL