g_func.go 3.2 KB

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