From ecacad737ac436ae2a5f000645b6f0693879c2a6 Mon Sep 17 00:00:00 2001 From: Zpekii Date: Sat, 10 Jan 2026 17:19:14 +0800 Subject: [PATCH] feat: add utility function to log caller information and clean up handler --- cmn/utils/utils.go | 21 +++++++++++++++++++++ hello/hello.go | 12 ++---------- 2 files changed, 23 insertions(+), 10 deletions(-) create mode 100644 cmn/utils/utils.go diff --git a/cmn/utils/utils.go b/cmn/utils/utils.go new file mode 100644 index 0000000..3c57778 --- /dev/null +++ b/cmn/utils/utils.go @@ -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) +} diff --git a/hello/hello.go b/hello/hello.go index f71e7fa..7e24e63 100644 --- a/hello/hello.go +++ b/hello/hello.go @@ -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") - - } - } - } }