I compile a program with Go for various platforms and run it by calling a relative path or just by its name (if it is in the PATH variable).
Is it possible to find out where the executable is?
Say, my program is called "foo
(.exe
)". I can run ./foo
, foo
(if it's in the PATH), ../../subdir/subdir/foo
.
I have tried to use os.Args[0]
and I guess I should check if the program name contains something different besides "foo". If yes, use filepath.Abs
, if no, use (I can't find the function name, there is a function that looks through the PATH to check where the program is).
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…