github.com/gogf/gf@v1.16.9/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/gogf/gf.
     6  
     7  package g
     8  
     9  import (
    10  	"context"
    11  	"github.com/gogf/gf/container/gvar"
    12  	"github.com/gogf/gf/internal/empty"
    13  	"github.com/gogf/gf/net/ghttp"
    14  	"github.com/gogf/gf/os/gproc"
    15  	"github.com/gogf/gf/util/gutil"
    16  )
    17  
    18  // NewVar returns a gvar.Var.
    19  func NewVar(i interface{}, safe ...bool) *Var {
    20  	return gvar.New(i, safe...)
    21  }
    22  
    23  // Wait is an alias of ghttp.Wait, which blocks until all the web servers shutdown.
    24  // It's commonly used in multiple servers situation.
    25  func Wait() {
    26  	ghttp.Wait()
    27  }
    28  
    29  // Listen is an alias of gproc.Listen, which handles the signals received and automatically
    30  // calls registered signal handler functions.
    31  // It blocks until shutdown signals received and all registered shutdown handlers done.
    32  func Listen() {
    33  	gproc.Listen()
    34  }
    35  
    36  // Dump dumps a variable to stdout with more manually readable.
    37  func Dump(i ...interface{}) {
    38  	gutil.Dump(i...)
    39  }
    40  
    41  // Export exports a variable to string with more manually readable.
    42  func Export(i ...interface{}) string {
    43  	return gutil.Export(i...)
    44  }
    45  
    46  // Throw throws a exception, which can be caught by TryCatch function.
    47  // It always be used in TryCatch function.
    48  func Throw(exception interface{}) {
    49  	gutil.Throw(exception)
    50  }
    51  
    52  // Try implements try... logistics using internal panic...recover.
    53  // It returns error if any exception occurs, or else it returns nil.
    54  func Try(try func()) (err error) {
    55  	return gutil.Try(try)
    56  }
    57  
    58  // TryCatch implements try...catch... logistics using internal panic...recover.
    59  // It automatically calls function <catch> if any exception occurs ans passes the exception as an error.
    60  func TryCatch(try func(), catch ...func(exception error)) {
    61  	gutil.TryCatch(try, catch...)
    62  }
    63  
    64  // IsNil checks whether given <value> is nil.
    65  // Parameter <traceSource> is used for tracing to the source variable if given <value> is type
    66  // of a pinter that also points to a pointer. It returns nil if the source is nil when <traceSource>
    67  // is true.
    68  // Note that it might use reflect feature which affects performance a little bit.
    69  func IsNil(value interface{}, traceSource ...bool) bool {
    70  	return empty.IsNil(value, traceSource...)
    71  }
    72  
    73  // IsEmpty checks whether given <value> empty.
    74  // It returns true if <value> is in: 0, nil, false, "", len(slice/map/chan) == 0.
    75  // Or else it returns true.
    76  func IsEmpty(value interface{}) bool {
    77  	return empty.IsEmpty(value)
    78  }
    79  
    80  // RequestFromCtx retrieves and returns the Request object from context.
    81  func RequestFromCtx(ctx context.Context) *ghttp.Request {
    82  	return ghttp.RequestFromCtx(ctx)
    83  }