是否有可能获得有关Golang中调用函数的信息?

时间:2016-02-04 22:34:16

标签: go

是否可以在Golang中获取有关调用者函数的信息?例如,如果我有

func foo() {
    //Do something
}
func main() {
    foo() 
}

如何从foo调用main? 我能用其他语言(例如在C#中我只需要使用CallerMemberName类属性)

2 个答案:

答案 0 :(得分:31)

您可以使用runtime.Caller轻松检索有关来电者的信息:

internal class DelegateCommand<T> : ICommand
{
    private Action<T> _clickAction;

    public DelegateCommand(Action<T> clickAction)
    {
        _clickAction = clickAction;
    }

    public event EventHandler CanExecuteChanged;

    public bool CanExecute(object parameter)
    {
        return true;
    }

    public void Execute(object parameter)
    {
        _clickAction((T)parameter);
    }
}

示例#1 :打印来电者文件名和行号:https://play.golang.org/p/cdO4Z4ApHS

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

示例#2:使用runtime.FuncForPC获取更多信息:https://play.golang.org/p/y8mpQq2mAv

package main

import (
    "fmt"
    "runtime"
)

func foo() {
    _, file, no, ok := runtime.Caller(1)
    if ok {
        fmt.Printf("called from %s#%d\n", file, no)
    }
}

func main() {
    foo()
}

答案 1 :(得分:13)

扩展我的评论,这里有一些代码返回当前func的调用者

import(
    "fmt"
    "runtime"
)

func getFrame(skipFrames int) runtime.Frame {
    // We need the frame at index skipFrames+2, since we never want runtime.Callers and getFrame
    targetFrameIndex := skipFrames + 2

    // Set size to targetFrameIndex+2 to ensure we have room for one more caller than we need
    programCounters := make([]uintptr, targetFrameIndex+2)
    n := runtime.Callers(0, programCounters)

    frame := runtime.Frame{Function: "unknown"}
    if n > 0 {
        frames := runtime.CallersFrames(programCounters[:n])
        for more, frameIndex := true, 0; more && frameIndex <= targetFrameIndex; frameIndex++ {
            var frameCandidate runtime.Frame
            frameCandidate, more = frames.Next()
            if frameIndex == targetFrameIndex {
                frame = frameCandidate
            }
        }
    }

    return frame
}

// MyCaller returns the caller of the function that called it :)
func MyCaller() string {
        // Skip GetCallerFunctionName and the function to get the caller of
        return getFrame(2).Function
}

// foo calls MyCaller
func foo() {
    fmt.Println(MyCaller())
}

// bar is what we want to see in the output - it is our "caller"
func bar() {
    foo()
}

func main(){
    bar()
}

更多示例:https://play.golang.org/p/cv-SpkvexuM