go.go raw

   1  package xsync
   2  
   3  import (
   4  	"fmt"
   5  	"runtime/debug"
   6  )
   7  
   8  // Go allows running a function in another goroutine
   9  // and waiting for its error.
  10  func Go(fn func() error) <-chan error {
  11  	errs := make(chan error, 1)
  12  	go func() {
  13  		defer func() {
  14  			r := recover()
  15  			if r != nil {
  16  				select {
  17  				case errs <- fmt.Errorf("panic in go fn: %v, %s", r, debug.Stack()):
  18  				default:
  19  				}
  20  			}
  21  		}()
  22  		errs <- fn()
  23  	}()
  24  
  25  	return errs
  26  }
  27