(1) Write a short function calling runtime.Caller ()
(2) Call this function wherever you want to access the source code file and line number at run time.
Example:
import "runtime" func file_line() string { _, fileName, fileLine, ok := runtime.Caller(1) var s string if ok { s = fmt.Sprintf("%s:%d", fileName, fileLine) } else { s = "" } return s }
Note: pass 1 to Caller () so that it returns the line number where the file_line () function is called, and not where runtime.Caller () is called.
fmt.Println(file_line()) // Prints this file and line number.
Koala3
source share