gdebug_grid.go 859 B

1234567891011121314151617181920212223242526272829
  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 gdebug
  7. import (
  8. "regexp"
  9. "runtime"
  10. "strconv"
  11. )
  12. var (
  13. // gridRegex is the regular expression object for parsing goroutine id from stack information.
  14. gridRegex = regexp.MustCompile(`^\w+\s+(\d+)\s+`)
  15. )
  16. // GoroutineId retrieves and returns the current goroutine id from stack information.
  17. // Be very aware that, it is with low performance as it uses runtime.Stack function.
  18. // It is commonly used for debugging purpose.
  19. func GoroutineId() int {
  20. buf := make([]byte, 26)
  21. runtime.Stack(buf, false)
  22. match := gridRegex.FindSubmatch(buf)
  23. id, _ := strconv.Atoi(string(match[1]))
  24. return id
  25. }