-
Notifications
You must be signed in to change notification settings - Fork 0
/
gdebug_grid.go
29 lines (25 loc) · 859 Bytes
/
gdebug_grid.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
//
// 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,
// You can obtain one at https://github.com/gogf/gf.
package gdebug
import (
"regexp"
"runtime"
"strconv"
)
var (
// gridRegex is the regular expression object for parsing goroutine id from stack information.
gridRegex = regexp.MustCompile(`^\w+\s+(\d+)\s+`)
)
// GoroutineId retrieves and returns the current goroutine id from stack information.
// Be very aware that, it is with low performance as it uses runtime.Stack function.
// It is commonly used for debugging purpose.
func GoroutineId() int {
buf := make([]byte, 26)
runtime.Stack(buf, false)
match := gridRegex.FindSubmatch(buf)
id, _ := strconv.Atoi(string(match[1]))
return id
}