In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains "how to get the function name of the caller in the function of Go". The content of the explanation in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to get the function name of the caller in the function of Go".
Func Foo () {fmt.Println (who is calling me?) Bar ()} func Bar () {fmt.Println ("who's calling me?") }
First print the name of the function itself
The easiest way is to hard code. Because before compiling, we definitely know which function to print in, but it's better to write a general function, such as the following example:
Package main import ("fmt"runtime") func main () {Foo ()} func Foo () {fmt.Printf ("I am% s, who is calling me?\ n", printMyName ()) Bar ()} func Bar () {fmt.Printf ("I am% s, who is calling me?\ n", printMyName ()} func printMyName () string {pc, _, _ = runtime.Caller (1) return runtime.FuncForPC (pc) .Name ()}
Output result:
I'm main.Foo. Who's calling me? I'm main.Bar. Who's calling me?
You can see that when the function is called, printMyName prints out the name of the function itself. Notice that the parameter of Caller is 1, because we encapsulate the business code into a function.
First print the name of the function caller
Modify the above code to add a new printCallerName function to print the name of the caller.
Func main () {Foo ()} func Foo () {fmt.Printf ("I am% s,% s calling me!\ n", printMyName (), printCallerName () Bar ()} func Bar () {fmt.Printf ("I am% s,% s calling me again!\ n", printMyName (), printCallerName ()} func printMyName () string {pc, _, _ = runtime.Caller (1) return runtime.FuncForPC (pc). Name ()} func printCallerName () string {pc, _: = runtime.Caller (2) return runtime.FuncForPC (pc). Name ()}
Introduction of correlation function
You can trace the call stack of functions in more detail through runtime.Caller, runtime.Callers, runtime.FuncForPC and other functions.
1. Func Caller (skip int) (pc uintptr, file string, line int, ok bool)
Caller can return program counters, file information, and line numbers at a certain level of the function call stack.
0 represents the current function, which is also the function that calls runtime.Caller. 1 represents the caller at the upper level, and so on.
2. Func Callers (skip int, pc [] uintptr) int
Callers is used to return the program counter of the calling station and put it in a uintptr.
0 represents Callers itself, which is different from the meaning of the parameters of Caller above, caused by historical reasons. 1 corresponds to the 0 above.
For example, in the above example, add a trace function, which is called by the function Bar.
…… Func Bar () {fmt.Printf ("I am% s,% s is calling me again!\ n", printMyName (), printCallerName () trace ()} func trace () {pc: = make ([] uintptr, 10) / / at least 1 entry needed n: = runtime.Callers (0, pc) for I: = 0; I < n Line + {f: = runtime.FuncForPC (PC [I]) file, line: = f.FileLine (PC [I]) fmt.Printf ("% slug% d% s\ n", file, line, f.Name ())}}
As you can see from the output, the entire stack of this goroutine is printed:
/ usr/local/go/src/runtime/extern.go:218 runtime.Callers / Users/yuepan/go/src/git.intra.weibo.com/platform/tool/g/main.go:34 main.trace / Users/yuepan/go/src/git.intra.weibo.com/platform/tool/g/main.go:20 main.Bar / Users/yuepan/go/src/git.intra.weibo.com/platform/tool/g/main.go:15 main.Foo / Users/yuepan/go/src / git.intra.weibo.com/platform/tool/g/main.go:10 main.main / usr/local/go/src/runtime/proc.go:210 runtime.main / usr/local/go/src/runtime/asm_amd64.s:1334 runtime.goexit
3. Func CallersFrames (callers [] uintptr) * Frames
The Callers above is just the program counter of the stack. If you want to get the information about the entire stack, you can use the CallersFrames function to avoid traversing the calling FuncForPC.
The above trace function can be changed to the following way:
Func trace2 () {pc: = make ([] uintptr, 10) / / at least 1 entry needed n: = runtime.Callers (0, pc) frames: = runtime.CallersFrames (pc [: n]) for {frame, more: = frames.Next () fmt.Printf ("% SV% d% s\ n", frame.File, frame.Line, frame.Function) if! more {break}
4. Func FuncForPC (pc uintptr) * Func
FuncForPC is an interesting function that can get the information of the function corresponding to the address of the program counter. If the inline program counter corresponds to more than one function, it returns the outermost function.
Its return value is a value of type * Func. You can get the function address, file line, function name and other information through * Func.
In addition to the way the program counter is obtained above, the address of the function can also be obtained by reflection:
Runtime.FuncForPC (reflect.ValueOf (foo). Pointer ()). Name ()
5. Get the program stack
In the program panic, the stack is usually typed out automatically. If you want to get the stack information in the program, you can print it through debug.PrintStack (). For example, if you encounter an Error in a program, but don't expect the program panic, but just want to print out the stack information for tracking debugging, you can use debug.PrintStack ().
Or, read the stack information yourself, process it and print it yourself:
Func DumpStacks () {buf: = make ([] byte, 16384) buf = buf [: runtime.Stack (buf, true)] fmt.Printf ("= BEGIN goroutine stack dump = =\ n% s\ nregions = END goroutine stack dump = =", buf)}
Refer to the debugging tool: dump goroutine's stacktrace.
The id of goroutine can also be obtained by using stack information. Refer to: let's talk about the method of getting goroutine id.
Func GoID () int {var buf [64] byte n: = runtime.Stack (buf [:], false) idField: = strings.Fields (strings.TrimPrefix (string (buf [: n]), "goroutine")) [0] id, err: = strconv.Atoi (idField) if err! = nil {panic (fmt.Sprintf ("cannot get goroutine id:% v", err)} return id} thank you for your reading The above is the content of "how to get the function name of the caller in the function of Go". After the study of this article, I believe you have a deeper understanding of how to get the function name of the caller in the function of Go. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.