forked from gookit/goutil
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgoutil.go
60 lines (51 loc) · 1.36 KB
/
goutil.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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// Package goutil Useful utils for the Go: int, string, array/slice, map, format, cli, env, filesystem, testing and more.
package goutil
import (
"fmt"
"reflect"
"runtime"
"strings"
"github.com/gookit/goutil/stdutil"
)
// PanicIfErr if error is not empty
func PanicIfErr(err error) {
if err != nil {
panic(err)
}
}
// Panicf format panic message use fmt.Sprintf
func Panicf(format string, v ...interface{}) {
panic(fmt.Sprintf(format, v...))
}
// FuncName get func name
func FuncName(f interface{}) string {
return runtime.FuncForPC(reflect.ValueOf(f).Pointer()).Name()
}
// PkgName get current package name
//
// Usage:
// funcName := goutil.FuncName(fn)
// pgkName := goutil.PkgName(funcName)
func PkgName(funcName string) string {
for {
lastPeriod := strings.LastIndex(funcName, ".")
lastSlash := strings.LastIndex(funcName, "/")
if lastPeriod > lastSlash {
funcName = funcName[:lastPeriod]
} else {
break
}
}
return funcName
}
// GetCallStacks stacks is a wrapper for runtime.
// Stack that attempts to recover the data for all goroutines.
// from glog package
func GetCallStacks(all bool) []byte {
return stdutil.GetCallStacks(all)
}
// GetCallersInfo returns an array of strings containing the file and line number
// of each stack frame leading
func GetCallersInfo(skip, max int) (callers []string) {
return stdutil.GetCallersInfo(skip, max)
}