在Golang中调用代码的位置可以获取文件名吗? [英] Is it possible to get filename where code is called in Golang?

查看:615
本文介绍了在Golang中调用代码的位置可以获取文件名吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我能够获得当前目录的完整路径,现在我想创建一个函数来读取或获取执行代码的文件名。我能够得到文件名,但它返回的是代码写入的原始文件名:

  func GetFileName()string { 
_,fpath,_,ok:= runtime.Caller(0)
if!ok {
err:= errors.New(未能获取文件名)
panic (错误)
}
文件名:= filepath.Base(fpath)
//删除扩展名
文件名= removeExtension(文件名)
返回文件名+.log
}

我想要做的是获取当前的fileName, :



我创建了 app.go

  package my 

函数getCurrentFileName()string {
//这里有一些代码会返回代码执行的文件名。
}

然后当我调用 getCurrentFileName() code>放入不同文件(如 hello.go )的其他位置。它会返回 hello.go



我在这里呆了一段时间并寻找答案。 / p>

解决方案

基本上,这是你告诉/传递给 runtime.Caller() :返回条目前要跳过的堆栈条目数量。

b
$ b

如果您在代码中传递 0 ,则表示返回堆栈条目,其中 runtime.Caller() 被调用(其中 you 称为 runtime.Caller())。传递 1 会跳过你的函数,并返回调用你的函数的函数:

  pc,file,line,ok:= runtime.Caller(1)
if ok {
fmt.Printf(从%s调用,行#%d,func:%v \
文件,行,runtime.FuncForPC(pc).Name())
}

示例调用包含此函数的函数(在我的示例中为 subplay.A()):

  7 func main(){
8 //注释行
9 subplay.A()
10}



$ b $输出:

$ p $ 从/ home / icza / gows / src / play / play.go,第9行,func:main.main

我们看到代码从函数 main()打印出 play.go 在第9行调用了我们的函数 main 包的


I was able to get the full path of the current directory, Now I would like to create a function that will read or get the filename where the code is executed. I am able to get the filename but it is return the original filename where the code is written :

func GetFileName() string {
    _, fpath, _, ok := runtime.Caller(0)
    if !ok {
        err := errors.New("failed to get filename")
        panic(err)
    }
    filename := filepath.Base(fpath)
    // remove extension
    filename = removeExtension(filename)
    return filename + ".log"
}

What I want to do is getting the current fileName where the code is executed like :

I created app.go :

package my

function getCurrentFileName() string {
    // some code here that will return the filename where this code is executed.
}

and then when I call getCurrentFileName() in a different file like hello.go in a different location. it will return hello.go.

I have been stuck here for a while and looking for an answer.

解决方案

Basically this is what you tell / pass to runtime.Caller(): the number of stack entries to skip before returning an entry.

If you pass 0 as in your code, that means return the stack entry where runtime.Caller() is called (where you called runtime.Caller()). Passing 1 will skip your function, and return the function that called your function:

pc, file, line, ok := runtime.Caller(1)
if ok {
    fmt.Printf("Called from %s, line #%d, func: %v\n",
        file, line, runtime.FuncForPC(pc).Name())
}

Example calling a function that contains this (subplay.A() in my example):

 7   func main() {
 8      // Comment line
 9      subplay.A()
10   }

Output:

Called from /home/icza/gows/src/play/play.go, line #9, func: main.main

We see that the code prints that play.go called our function at line #9, from the function main() of the main package.

这篇关于在Golang中调用代码的位置可以获取文件名吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆