github.com/wangyougui/gf/v2@v2.6.5/frame/g/g_func.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 g
     8  
     9  import (
    10  	"context"
    11  	"io"
    12  
    13  	"github.com/wangyougui/gf/v2/container/gvar"
    14  	"github.com/wangyougui/gf/v2/internal/empty"
    15  	"github.com/wangyougui/gf/v2/net/ghttp"
    16  	"github.com/wangyougui/gf/v2/os/gproc"
    17  	"github.com/wangyougui/gf/v2/util/gutil"
    18  )
    19  
    20  // Go creates a new asynchronous goroutine function with specified recover function.
    21  //
    22  // The parameter `recoverFunc` is called when any panic during executing of `goroutineFunc`.
    23  // If `recoverFunc` is given nil, it ignores the panic from `goroutineFunc` and no panic will
    24  // throw to parent goroutine.
    25  //
    26  // But, note that, if `recoverFunc` also throws panic, such panic will be thrown to parent goroutine.
    27  func Go(
    28  	ctx context.Context,
    29  	goroutineFunc func(ctx context.Context),
    30  	recoverFunc func(ctx context.Context, exception error),
    31  ) {
    32  	gutil.Go(ctx, goroutineFunc, recoverFunc)
    33  }
    34  
    35  // NewVar returns a gvar.Var.
    36  func NewVar(i interface{}, safe ...bool) *Var {
    37  	return gvar.New(i, safe...)
    38  }
    39  
    40  // Wait is an alias of ghttp.Wait, which blocks until all the web servers shutdown.
    41  // It's commonly used in multiple servers' situation.
    42  func Wait() {
    43  	ghttp.Wait()
    44  }
    45  
    46  // Listen is an alias of gproc.Listen, which handles the signals received and automatically
    47  // calls registered signal handler functions.
    48  // It blocks until shutdown signals received and all registered shutdown handlers done.
    49  func Listen() {
    50  	gproc.Listen()
    51  }
    52  
    53  // Dump dumps a variable to stdout with more manually readable.
    54  func Dump(values ...interface{}) {
    55  	gutil.Dump(values...)
    56  }
    57  
    58  // DumpTo writes variables `values` as a string in to `writer` with more manually readable
    59  func DumpTo(writer io.Writer, value interface{}, option gutil.DumpOption) {
    60  	gutil.DumpTo(writer, value, option)
    61  }
    62  
    63  // DumpWithType acts like Dump, but with type information.
    64  // Also see Dump.
    65  func DumpWithType(values ...interface{}) {
    66  	gutil.DumpWithType(values...)
    67  }
    68  
    69  // DumpWithOption returns variables `values` as a string with more manually readable.
    70  func DumpWithOption(value interface{}, option gutil.DumpOption) {
    71  	gutil.DumpWithOption(value, option)
    72  }
    73  
    74  // DumpJson pretty dumps json content to stdout.
    75  func DumpJson(jsonContent string) {
    76  	gutil.DumpJson(jsonContent)
    77  }
    78  
    79  // Throw throws an exception, which can be caught by TryCatch function.
    80  func Throw(exception interface{}) {
    81  	gutil.Throw(exception)
    82  }
    83  
    84  // Try implements try... logistics using internal panic...recover.
    85  // It returns error if any exception occurs, or else it returns nil.
    86  func Try(ctx context.Context, try func(ctx context.Context)) (err error) {
    87  	return gutil.Try(ctx, try)
    88  }
    89  
    90  // TryCatch implements try...catch... logistics using internal panic...recover.
    91  // It automatically calls function `catch` if any exception occurs and passes the exception as an error.
    92  //
    93  // But, note that, if function `catch` also throws panic, the current goroutine will panic.
    94  func TryCatch(ctx context.Context, try func(ctx context.Context), catch func(ctx context.Context, exception error)) {
    95  	gutil.TryCatch(ctx, try, catch)
    96  }
    97  
    98  // IsNil checks whether given `value` is nil.
    99  // Parameter `traceSource` is used for tracing to the source variable if given `value` is type
   100  // of pointer that also points to a pointer. It returns nil if the source is nil when `traceSource`
   101  // is true.
   102  // Note that it might use reflect feature which affects performance a little.
   103  func IsNil(value interface{}, traceSource ...bool) bool {
   104  	return empty.IsNil(value, traceSource...)
   105  }
   106  
   107  // IsEmpty checks whether given `value` empty.
   108  // It returns true if `value` is in: 0, nil, false, "", len(slice/map/chan) == 0.
   109  // Or else it returns true.
   110  //
   111  // The parameter `traceSource` is used for tracing to the source variable if given `value` is type of pointer
   112  // that also points to a pointer. It returns true if the source is empty when `traceSource` is true.
   113  // Note that it might use reflect feature which affects performance a little.
   114  func IsEmpty(value interface{}, traceSource ...bool) bool {
   115  	return empty.IsEmpty(value, traceSource...)
   116  }
   117  
   118  // RequestFromCtx retrieves and returns the Request object from context.
   119  func RequestFromCtx(ctx context.Context) *ghttp.Request {
   120  	return ghttp.RequestFromCtx(ctx)
   121  }