Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to get filename where code is called in Golang? [duplicate]

Tags:

file

go

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.

like image 226
Gujarat Santana Avatar asked Nov 10 '17 08:11

Gujarat Santana


People also ask

How do you check if a file exist in Golang?

In order to check if a particular file exists inside a given directory in Golang, we can use the Stat() and the isNotExists() function that the os package of Go's standard library provides us with. The Stat() function is used to return the file info structure describing the file.

How do I get file names to go?

In the Go programming language, to get the file name extension used by the given path – we use the Ext() function of path/filepath package. The Ext() function returns the file name extension used by the given path.

How do you get a path in Golang?

Getwd functionis used to get the current working directory in Golang, the function returns the rooted path name and if the current directory can be reached via multiple paths, the function can return any one of them. The func returns two things the directory and also error, if there is no error it returns nil.


1 Answers

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.

like image 200
icza Avatar answered Oct 22 '22 18:10

icza