Skip to content

dgnorton/goback

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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.

About

A simple non-database related transaction library

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages