github.com/gogf/gf/v2@v2.7.4/debug/gdebug/gdebug_grid.go (about)

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