Here is part of my registration package. It retrieves the caller information of the logging function to display later on the output.
func retrieveCallInfo() *callInfo { pc, file, line, _ := runtime.Caller(2) _, fileName := path.Split(file) parts := strings.Split(runtime.FuncForPC(pc).Name(), ".") pl := len(parts) packageName := "" funcName := parts[pl-1] if parts[pl-2][0] == '(' { funcName = parts[pl-2] + "." + funcName packageName = strings.Join(parts[0:pl-2], ".") } else { packageName = strings.Join(parts[0:pl-1], ".") } return &callInfo{ packageName: packageName, fileName: fileName, funcName: funcName, line: line, } }
As you can see, it also returns the name of the package.
themue
source share