1 Star 0 Fork 0

xingyp/cn-infra

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
runtimeutils.go 1.29 KB
一键复制 编辑 原始数据 按行查看 历史
jozef.slezak 提交于 2017-07-17 14:45 . ODPM-419 fix golint issues
package runtimeutils
import (
"bytes"
"fmt"
"reflect"
"runtime"
"strconv"
"strings"
)
var goroutineSpace = []byte("goroutine ")
// GoroutineID returns current GO Routine ID (parsed from runtime.Stack)
func GoroutineID() uint64 {
b := make([]byte, 64)
b = b[:runtime.Stack(b, false)]
// Parse the 4707 out of "goroutine 4707 ["
b = bytes.TrimPrefix(b, goroutineSpace)
i := bytes.IndexByte(b, ' ')
if i < 0 {
panic(fmt.Sprintf("No space found in %q", b))
}
b = b[:i]
n, err := strconv.ParseUint(string(b), 10, 64)
if err != nil {
panic(fmt.Sprintf("Failed to parse goroutine ID out of %q: %v", b, err))
}
return n
}
// GetFunction returns metadata about function based on pointer to a function
//
// Example usage:
//
// func foo() {}
// GetFunction(foo)
func GetFunction(function interface{}) *runtime.Func {
return runtime.FuncForPC(reflect.ValueOf(function).Pointer())
}
// GetFunctionName returns name of the function
//
// Example usage:
//
// func foo() {}
// GetFunctionName(foo) // returns string containing "foo" substring
func GetFunctionName(function interface{}) string {
fullName := GetFunction(function).Name()
name := strings.TrimSuffix(fullName, "-fm")
dot := strings.LastIndex(name, ".")
if dot > 0 && dot+1 < len(name) {
return name[dot+1:]
}
return fullName
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/xingyp/cn-infra.git
git@gitee.com:xingyp/cn-infra.git
xingyp
cn-infra
cn-infra
v1.1.0

搜索帮助