package util import ( "sync" "testing" ) /** 对于非闭包函数的支持 */ func TestGoWithRecover(t *testing.T) { wg := sync.WaitGroup{} x := 0 wg.Add(1) GoWithRecover(func() { defer func() { wg.Done() }() t.Logf("run : %d\n", x) t.Log(10 / x) }, func(r interface{}) { t.Logf("find err: %s\n", r) }) wg.Wait() } /** 不支持闭包函数循环结构 */ func TestGoWithRecoverBiBao(t *testing.T) { wg := sync.WaitGroup{} x := 0 wg.Add(10) for n := 0; n < 10; n++ { GoWithRecover(func() { defer func() { wg.Done() }() t.Logf("run : %d\n", n) t.Log(10 / x) }, func(r interface{}) { t.Logf("find err: %s\n", r) }) } wg.Wait() }