SoFunction
Updated on 2025-03-05

Use in Go

Use in Go

The following describes Go usage


Function definition
func (skip int) (pc uintptr, file string, line int, ok bool)

effect
Get the functionCallerCall information

parameter
When skip=0: Returns the pc (program counter) of function A that calls Caller (A is the current function where the code is located), the file name, and the number of lines of Caller in function A
When skip=1: Returns the pc of function B that calls function A, the file name where it is located, and the number of lines of function A in function B; and so on

pc: Program counter, pointing to the next instruction address to fetch
file: The file path of the function corresponding to pc
line: The number of rows of the callee in the function corresponding to the pc

Example:
/Users/go/src/liyouming/golang-trick/40-clean-code-controll-flow/util/

package util
import (
	"bytes"
	"fmt"
	"runtime"
)
func CallerTest() {
	for i := 0; i <= 4; i++ {
		pc, file, line, ok := (i)
		if ok {
			("wheni:=%dhour:\nCaller'spc:%v\nCaller's函数名:%s\n"+
				"Where the caller isfile:%s\nThe callee'sline:%d\n", i, pc, (pc).Name(), file, line)
			(string(([]byte("*"), 10)))
		}
	}
}

package main
import (
	"golang-trick/40-clean-code-controll-flow/util"
)
func main() {
	()
}

The operation results are as follows:

When i:=0:
Caller's pc:17363077
The caller's function name: golang-trick/40-clean-code-control-flow/
The caller's file:/Users/go/src/liyouming/golang-trick/40-clean-code-control-flow/util/
The line of the callee in the caller:11
**********
When i:=1:
Caller's pc:17363702
The caller's function name:
The caller's file:/Users/go/src/liyouming/golang-trick/40-clean-code-control-flow/
The line of the callee in the caller:8
**********
When i:=2:
Caller's pc:16987729
The caller's function name:
The caller's file:/usr/local/opt/go/libexec/src/runtime/
The line of the callee in the caller:250
**********
When i:=3:
Caller's pc:17158656
The caller's function name:
The caller's file:/usr/local/opt/go/libexec/src/runtime/asm_amd64.s
The line of the callee in the caller:1594
**********

Replenish:

Go Learning - () function

function:

func Caller(skip int) (pc uintptr, file string, line int, ok bool)
Caller() reports the file and line number information of the functions executed by the current go program call stack.

Parameter explanation:

  • skip

The number of frames traced upwards, 0 represents the caller of Caller (the call stack where Caller is located) (0-current function, 1-previous layer function,…).

  • pc

Call stack identifier

  • file:

File path

  • line:

The line number of the call in the file

  • ok:

If information cannot be obtained, ok will be set to false

example:

Maybe I read the explanation above, forskipThe parameters are still confusing, let's take a look at an example:

The project directory structure at this time:

blog/
	├── conf /...
	├── 
	├── middleware /...
	├── models /...
	├── pkg
	│ 	├── e /...
	│ 	├── logging
	│ 	│ 	├── 
	│ 	│ 	└── 
	│ 	├── setting /...
	│ 	└── util /...
	├── routers
	│ 	├── api
	│ 	│ 	├── 
	│ 	│ 	└── v1
	│ 	│ 		├── 
	│ 	│ 		└── 
	│ 	└── 
	├── runtime

Just take itblog/routers/api/v1/When an example, in this fileGetArticle()Used in():

// followJianYuStudyGo/routers/api/v1/
func GetArticle(c *) {
	...
	} else {
		for _, err := range  {
			(, ) // :122 Number of frames on the upper stack skip = 2		}
	}
	...
}
// followJianYuStudyGo/pkg/logging/
func Info(v ...interface{}) {
	setPrefix(INFO) // :67 Number of frames on the upper stack skip = 1	(v)
}
func setPrefix(level Level) {
	_, file, line, ok := (DefaultCallerDepth) // :50 Number of frames on the upper stack skip = 0    ....
}

If oursskip

  • To be 0:

The number of frames on the upper trace is 0, and the returned onefileJust callCaller()Location:

[INFO][:50]2022/04/29 21:07:11 [created_by The creator cannot be empty]
  • For 1:

The number of frames on the upper trace is 1, and the returned onefileJust callCaller()The previous level location:

[INFO][:67]2022/04/29 21:25:57 [state The status is only allowed0or1]
  • For 2:

The number of frames on the upper trace is 2, and the returned onefileJust callCaller()The previous level location of the previous level:

[INFO][:122]2022/04/29 20:52:23 [state The status is only allowed0or1]

Key points:
Because we areloggingIn the bagFile encapsulatedInfo()、Debug()、Warn()、....Functions, other places call these encapsulated functions, so if we want to obtain them when printing the logs, we use them.()Location,(skip int)The skip is set to 2 (see the part where skip=2 for the reason)! ! ! !

This is all about this article about the use of Go. For more relevant content on using Go, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!