1 Star 0 Fork 0

Cruvie Kang/kk_go_kit

加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
func.go 1.02 KB
一键复制 编辑 原始数据 按行查看 历史
Cruvie Kang 提交于 2024-08-18 01:39 +08:00 . update
package kk_reflect
import (
"log/slog"
"reflect"
"runtime"
"strings"
)
// GetFunctionName make sure use this only for test or a dynamic function
// do not use this in production code
func GetFunctionName(function any, getFullName ...bool) string {
fullFuncName := runtime.FuncForPC(reflect.ValueOf(function).Pointer()).Name()
if len(getFullName) > 0 && getFullName[0] {
return fullFuncName
}
splitFuncName := strings.Split(fullFuncName, ".")
funcName := splitFuncName[len(splitFuncName)-1]
return funcName
}
// GetCurrentFunctionName make sure use this only for test or a dynamic function
// do not use this in production code
func GetCurrentFunctionName(getFullName ...bool) string {
pc, _, _, ok := runtime.Caller(1)
if !ok {
slog.Error("GetCurrentFunctionName error")
return ""
}
fullFuncName := runtime.FuncForPC(pc).Name()
if len(getFullName) > 0 && getFullName[0] {
return fullFuncName
}
splitFuncName := strings.Split(fullFuncName, ".")
funcName := splitFuncName[len(splitFuncName)-1]
return funcName
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/cruvie/kk_go_kit.git
git@gitee.com:cruvie/kk_go_kit.git
cruvie
kk_go_kit
kk_go_kit
v0.1.2

搜索帮助