feat: add utility function to log caller information and clean up handler
This commit is contained in:
21
cmn/utils/utils.go
Normal file
21
cmn/utils/utils.go
Normal file
@@ -0,0 +1,21 @@
|
||||
package utils //nolint:revive
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"runtime"
|
||||
)
|
||||
|
||||
// Fnc prints the caller function name, file and line number.
|
||||
func Fnc() {
|
||||
pc, file, line, ok := runtime.Caller(1)
|
||||
if !ok {
|
||||
fmt.Println("unable to get caller")
|
||||
return
|
||||
}
|
||||
fn := runtime.FuncForPC(pc)
|
||||
name := "unknown"
|
||||
if fn != nil {
|
||||
name = fn.Name()
|
||||
}
|
||||
fmt.Printf("%s %s:%d <----\n", name, file, line)
|
||||
}
|
||||
@@ -1,19 +1,11 @@
|
||||
package hello
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"k8s-example/cmn/utils"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func Handler(w http.ResponseWriter, _ *http.Request) {
|
||||
utils.Fnc()
|
||||
_, _ = w.Write([]byte("Hello, World!"))
|
||||
|
||||
if true {
|
||||
if true {
|
||||
if true {
|
||||
fmt.Println("Deeply nested condition")
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user