Go打印函数名/文件名/行号

package main
import ("fmt";"runtime")

/*
golang 的runtime库,提供Caller函数,可以返回运行时正在执行的文件名和行号:
函数定义:
func Caller(skip int) (pc uintptr, file string, line int, ok bool) {}

函数用法:
_, file, line, ok := runtime.Caller(0)
*/

func main() {
  funcName,file,line,ok := runtime.Caller(0)
  if(ok){
	fmt.Println("func name: " + runtime.FuncForPC(funcName).Name())
	fmt.Printf("file: %s, line: %d\n",file,line)
   }
}

你可能感兴趣的:(Go语言学习)