기본 콘텐츠로 건너뛰기

[golang] getFunName

 import (

"fmt"

"log"

"runtime"

)


// Debug prints a debug information to the log with file and line.

func DebugFunInfo(format string, a ...interface{}) {

funName := ""

pc, file, line, ok := runtime.Caller(1)

info := fmt.Sprintf(format, a...)

// https://stackoverflow.com/questions/35212985/is-it-possible-get-information-about-caller-function-in-golang

details := runtime.FuncForPC(pc)

if ok && details != nil {

funName = details.Name()

//fmt.Printf("called from %s\n", details.Name())

}


log.Println("debug", funName, "file:", file, "(", line, ") ", info)

}


func main() {

DebugFunInfo("start")

}


댓글