github.com/wangyougui/gf/v2@v2.6.5/util/gutil/gutil_goroutine.go (about)

     1  // Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
     2  //
     3  // This Source Code Form is subject to the terms of the MIT License.
     4  // If a copy of the MIT was not distributed with this file,
     5  // You can obtain one at https://github.com/wangyougui/gf.
     6  
     7  package gutil
     8  
     9  import (
    10  	"context"
    11  )
    12  
    13  // Go creates a new asynchronous goroutine function with specified recover function.
    14  //
    15  // The parameter `recoverFunc` is called when any panic during executing of `goroutineFunc`.
    16  // If `recoverFunc` is given nil, it ignores the panic from `goroutineFunc` and no panic will
    17  // throw to parent goroutine.
    18  //
    19  // But, note that, if `recoverFunc` also throws panic, such panic will be thrown to parent goroutine.
    20  func Go(
    21  	ctx context.Context,
    22  	goroutineFunc func(ctx context.Context),
    23  	recoverFunc func(ctx context.Context, exception error),
    24  ) {
    25  	if goroutineFunc == nil {
    26  		return
    27  	}
    28  	go TryCatch(ctx, goroutineFunc, recoverFunc)
    29  }