package main
import (
"fmt"
"runtime"
"strings"
)
func main() {
myTest()
}
func myTest() {
str := "hello , i am error"
myLog(str)
}
func myLog(str string) {
//func Caller(skip int) (pc uintptr, file string, line int, ok bool)
pc, file, line, ok := runtime.Caller(1)
names := strings.Split(file, "/")
if ok {
fun := runtime.FuncForPC(pc)
fmt.Println("file name :", names[len(names)-1])
fmt.Println("func name :", fun.Name())
fmt.Println("file line :", line)
}
}
输出:
file name : main.go
func name : main.myTest
file line : 16
有疑问加站长微信联系(非本文作者)