g_func.go 2.7 KB

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