@@ -9,13 +9,15 @@ package core
99import (
1010 "context"
1111 "errors"
12+ "runtime/debug"
1213 "strconv"
1314 "time"
1415)
1516
1617// Go runs f in a goroutine and recovers from any panics.
1718func Go (who string , f func ()) {
1819 go func () {
20+ debug .SetPanicOnFault (true )
1921 defer Recover (DontExit , who )
2022
2123 f ()
@@ -25,6 +27,7 @@ func Go(who string, f func()) {
2527// Go1 runs f(arg) in a goroutine and recovers from any panics.
2628func Go1 [T any ](who string , f func (T ), arg T ) {
2729 go func () {
30+ debug .SetPanicOnFault (true )
2831 defer Recover (DontExit , who )
2932
3033 f (arg )
@@ -34,6 +37,7 @@ func Go1[T any](who string, f func(T), arg T) {
3437// Go2 runs f(arg0,arg1) in a goroutine and recovers from any panics.
3538func Go2 [T0 any , T1 any ](who string , f func (T0 , T1 ), a0 T0 , a1 T1 ) {
3639 go func () {
40+ debug .SetPanicOnFault (true )
3741 defer Recover (DontExit , who )
3842
3943 f (a0 , a1 )
@@ -44,6 +48,7 @@ func Go2[T0 any, T1 any](who string, f func(T0, T1), a0 T0, a1 T1) {
4448// then calls cb in a separate goroutine, and recovers from any panics.
4549func Gg (who string , f func (), cb func ()) {
4650 go func () {
51+ debug .SetPanicOnFault (true )
4752 defer RecoverFn (who , cb )
4853
4954 f ()
@@ -53,12 +58,23 @@ func Gg(who string, f func(), cb func()) {
5358// Gx runs f in a goroutine and exits the process if f panics.
5459func Gx (who string , f func ()) {
5560 go func () {
61+ debug .SetPanicOnFault (true )
5662 defer Recover (Exit11 , who )
5763
5864 f ()
5965 }()
6066}
6167
68+ // Gx1 runs f in a goroutine and exits the process if f panics.
69+ func Gx1 [T any ](who string , f func (T ), arg T ) {
70+ go func () {
71+ debug .SetPanicOnFault (true )
72+ defer Recover (Exit11 , who )
73+
74+ f (arg )
75+ }()
76+ }
77+
6278// Gif runs f in a goroutine if cond is true.
6379func Gif (cond bool , who string , f func ()) {
6480 if cond {
@@ -75,6 +91,7 @@ func Grx[T any](who string, f WorkCtx[T], d time.Duration) (zz T, completed bool
7591
7692 // go.dev/play/p/VtWYJrxhXz6
7793 go func () {
94+ debug .SetPanicOnFault (true )
7895 defer Recover (Exit11 , who )
7996 defer close (ch )
8097
@@ -90,6 +107,16 @@ func Grx[T any](who string, f WorkCtx[T], d time.Duration) (zz T, completed bool
90107 return zz , false
91108}
92109
110+ // Gxe runs f in a goroutine, ignores returned error, and exits on panics.
111+ func Gxe (who string , f func () error ) {
112+ go func () {
113+ debug .SetPanicOnFault (true )
114+ defer Recover (Exit11 , who )
115+
116+ _ = f ()
117+ }()
118+ }
119+
93120// errPanic returns an error indicating that the function at index i panicked.
94121func errPanic (who string ) error {
95122 return errors .New (who + " fn panicked" )
@@ -154,7 +181,7 @@ loop:
154181func First [T any ](who string , overallTimeout time.Duration , fs ... WorkCtx [T ]) (zz T , idx int ) {
155182 timeoutPerFn := overallTimeout / time .Duration (len (fs ))
156183 for i , f := range fs {
157- i , f := i , f
184+ // unneeded in go1.23+ i, f := i, f
158185 fid := who + ".all." + strconv .Itoa (i )
159186 if x , ok := Grx (fid , f , timeoutPerFn ); ok {
160187 return x , i
@@ -176,7 +203,7 @@ func All[T any](who string, timeout time.Duration, fs ...WorkCtx[T]) ([]T, []err
176203 defer cancel ()
177204
178205 for i , f := range fs {
179- i , f := i , f
206+ //unneeded in go1.23+ i, f := i, f
180207 fid := who + ".all." + strconv .Itoa (i )
181208 Gg (fid , func () {
182209 out , err := f (ctx )
0 commit comments