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.