2021-01-17 21:46:25 +08:00
|
|
|
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
|
2018-09-26 09:58:49 +08:00
|
|
|
//
|
|
|
|
// This Source Code Form is subject to the terms of the MIT License.
|
|
|
|
// If a copy of the MIT was not distributed with this file,
|
2019-02-02 16:18:25 +08:00
|
|
|
// You can obtain one at https://github.com/gogf/gf.
|
2018-09-26 09:58:49 +08:00
|
|
|
|
|
|
|
package g
|
|
|
|
|
|
|
|
import (
|
2021-07-13 23:01:31 +08:00
|
|
|
"context"
|
2021-11-13 23:23:55 +08:00
|
|
|
|
2021-10-11 21:41:56 +08:00
|
|
|
"github.com/gogf/gf/v2/container/gvar"
|
|
|
|
"github.com/gogf/gf/v2/internal/empty"
|
|
|
|
"github.com/gogf/gf/v2/net/ghttp"
|
|
|
|
"github.com/gogf/gf/v2/os/gproc"
|
|
|
|
"github.com/gogf/gf/v2/util/gutil"
|
2018-09-26 09:58:49 +08:00
|
|
|
)
|
|
|
|
|
2020-06-16 17:38:05 +08:00
|
|
|
// NewVar returns a gvar.Var.
|
2020-06-29 13:40:19 +08:00
|
|
|
func NewVar(i interface{}, safe ...bool) *Var {
|
2019-07-23 23:20:27 +08:00
|
|
|
return gvar.New(i, safe...)
|
2018-10-09 13:33:00 +08:00
|
|
|
}
|
|
|
|
|
2021-01-22 12:57:21 +08:00
|
|
|
// Wait is an alias of ghttp.Wait, which blocks until all the web servers shutdown.
|
2021-09-16 20:57:59 +08:00
|
|
|
// It's commonly used in multiple servers' situation.
|
2018-09-26 09:58:49 +08:00
|
|
|
func Wait() {
|
2019-06-19 09:06:52 +08:00
|
|
|
ghttp.Wait()
|
2021-01-22 12:57:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Listen is an alias of gproc.Listen, which handles the signals received and automatically
|
|
|
|
// calls registered signal handler functions.
|
|
|
|
// It blocks until shutdown signals received and all registered shutdown handlers done.
|
|
|
|
func Listen() {
|
2021-01-21 21:14:17 +08:00
|
|
|
gproc.Listen()
|
2018-09-26 09:58:49 +08:00
|
|
|
}
|
|
|
|
|
2019-02-27 22:17:09 +08:00
|
|
|
// Dump dumps a variable to stdout with more manually readable.
|
2021-10-21 22:07:43 +08:00
|
|
|
func Dump(values ...interface{}) {
|
|
|
|
gutil.Dump(values...)
|
2018-09-26 09:58:49 +08:00
|
|
|
}
|
2018-11-06 13:53:06 +08:00
|
|
|
|
2021-10-30 20:35:55 +08:00
|
|
|
// DumpWithType acts like Dump, but with type information.
|
2021-10-21 22:07:43 +08:00
|
|
|
// Also see Dump.
|
2021-10-30 20:35:55 +08:00
|
|
|
func DumpWithType(values ...interface{}) {
|
|
|
|
gutil.DumpWithType(values...)
|
2019-03-21 18:21:53 +08:00
|
|
|
}
|
|
|
|
|
2021-09-16 20:57:59 +08:00
|
|
|
// Throw throws an exception, which can be caught by TryCatch function.
|
2018-11-06 18:53:25 +08:00
|
|
|
func Throw(exception interface{}) {
|
2019-06-19 09:06:52 +08:00
|
|
|
gutil.Throw(exception)
|
2018-11-06 13:53:06 +08:00
|
|
|
}
|
|
|
|
|
2020-10-20 13:36:43 +08:00
|
|
|
// Try implements try... logistics using internal panic...recover.
|
|
|
|
// It returns error if any exception occurs, or else it returns nil.
|
|
|
|
func Try(try func()) (err error) {
|
|
|
|
return gutil.Try(try)
|
|
|
|
}
|
|
|
|
|
|
|
|
// TryCatch implements try...catch... logistics using internal panic...recover.
|
2021-09-16 20:57:59 +08:00
|
|
|
// It automatically calls function `catch` if any exception occurs ans passes the exception as an error.
|
2020-10-20 13:36:43 +08:00
|
|
|
func TryCatch(try func(), catch ...func(exception error)) {
|
2019-06-19 09:06:52 +08:00
|
|
|
gutil.TryCatch(try, catch...)
|
2019-03-21 18:20:20 +08:00
|
|
|
}
|
|
|
|
|
2021-09-16 20:57:59 +08:00
|
|
|
// IsNil checks whether given `value` is nil.
|
2021-10-21 18:22:47 +08:00
|
|
|
// Parameter `traceSource` is used for tracing to the source variable if given `value` is type
|
2021-09-16 20:57:59 +08:00
|
|
|
// of pinter that also points to a pointer. It returns nil if the source is nil when `traceSource`
|
2021-01-09 21:05:47 +08:00
|
|
|
// is true.
|
2021-09-16 20:57:59 +08:00
|
|
|
// Note that it might use reflect feature which affects performance a little.
|
2021-01-09 21:05:47 +08:00
|
|
|
func IsNil(value interface{}, traceSource ...bool) bool {
|
|
|
|
return empty.IsNil(value, traceSource...)
|
2020-02-23 20:25:55 +08:00
|
|
|
}
|
|
|
|
|
2021-09-16 20:57:59 +08:00
|
|
|
// IsEmpty checks whether given `value` empty.
|
|
|
|
// It returns true if `value` is in: 0, nil, false, "", len(slice/map/chan) == 0.
|
2020-02-23 20:25:55 +08:00
|
|
|
// Or else it returns true.
|
2019-03-21 18:20:20 +08:00
|
|
|
func IsEmpty(value interface{}) bool {
|
2019-06-19 09:06:52 +08:00
|
|
|
return empty.IsEmpty(value)
|
|
|
|
}
|
2021-07-13 23:01:31 +08:00
|
|
|
|
|
|
|
// RequestFromCtx retrieves and returns the Request object from context.
|
|
|
|
func RequestFromCtx(ctx context.Context) *ghttp.Request {
|
|
|
|
return ghttp.RequestFromCtx(ctx)
|
|
|
|
}
|