safely

package
v1.3.0 Latest Latest
Warning

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

Go to latest
Published: Sep 21, 2015 License: MIT Imports: 1 Imported by: 114

README

Safely: Do scary things safely in Go.

This is a small utility library for doing things safely.

Go Routine Wrappers

Don't let a panic in a goroutine take down your entire program.

import "github.com/Masterminds/safely"

func main() {
  safely.Go(func() { panic("Oops!") })
}

Documentation

Overview

Safely is a package for providing safety wrappers around commonly used features.

You might have at one point done this:

go myFunc()

But what happens if myFunc panics? It will terminate the program with a panic.

Safely provides a panic-trapping goroutine runner:

safely.Go(myFunc)

If `myFunc` panics, Safely will capture the panic and log the error message.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Go

func Go(todo GoDoer)

Go executes a function as a goroutine, but recovers from any panics.

Normally, if a GoRoutine panics, it will stop execution on the current program, which ain't always good.

safely.Go handles this by trapping the panic and writing it to the default logger.

To use your own logger, use safely.GoLog.

func GoDo

func GoDo(cxt Logger, todo GoDoer)

GoDo runs a Goroutine, traps panics, and logs panics to a safely.Logger.

Example:

_, _, cxt := cookoo.Cookoo()
safely.GoDo(cxt, func(){})

func GoLog

func GoLog(logger SafelyLogger, todo GoDoer)

GoLog executes a function as a goroutine, but traps any panics.

If a panic is encountered, it is logged to the given logger.

Types

type GoDoer

type GoDoer func()

GoDoer is a function that `safely.Go` can execute.

type Logger

type Logger interface {
	Logf(string, string, ...interface{})
}

Captures the log portion of a cookoo.Context.

type SafelyLogger

type SafelyLogger interface {
	Printf(string, ...interface{})
}

SafelyLogger is used to log messages when a failure occurs

Jump to

Keyboard shortcuts

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