g_func.go 4.3 KB

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