你如何确定当前正在运行的可执行文件的完整路径在go中?

huangapple go评论141阅读模式
英文:

How do you determine the full path of the currently running executable in go?

问题

我一直在OSX上使用这个函数:

  1. // 获取当前可执行文件路径的快捷方式
  2. func ExecPath() string {
  3. var here = os.Args[0]
  4. if !strings.HasPrefix(here, "/") {
  5. here, _ = exec.LookPath(os.Args[0])
  6. if !strings.HasPrefix(here, "/") {
  7. var wd, _ = os.Getwd()
  8. here = path.Join(wd, here)
  9. }
  10. }
  11. return here
  12. }

...但它非常混乱,并且在Windows上根本不起作用,尤其是在git-bash上也不行。

有没有一种跨平台的方法来实现这个?

注意:具体来说,args[0]取决于二进制文件的调用方式;在某些情况下,它只是二进制文件本身,例如"app"或"app.exe";所以你不能只使用它。

英文:

I've been using this function on osx:

  1. // Shortcut to get the path to the current executable
  2. func ExecPath() string {
  3. var here = os.Args[0]
  4. if !strings.HasPrefix(here, "/") {
  5. here, _ = exec.LookPath(os.Args[0])
  6. if !strings.HasPrefix(here, "/") {
  7. var wd, _ = os.Getwd()
  8. here = path.Join(wd, here)
  9. }
  10. }
  11. return here
  12. }

...but its pretty messy, and it doesn't work on windows at all, and certainly not in git-bash on windows.

Is there a way of doing this cross platform?

NB. Specifically that args[0] depends on how the binary is invoked; it is in some cases only the binary itself, eg. "app" or "app.exe"; so you can't just use that.

答案1

得分: 4

这是我认为在任何平台下都能正常工作的传统方法。

  1. import (
  2. "fmt"
  3. "os"
  4. "path/filepath"
  5. )
  6. // 获取当前可执行文件的路径的快捷方式
  7. func ExecPath() string {
  8. var here = os.Args[0]
  9. here, err := filepath.Abs(here)
  10. if err != nil {
  11. fmt.Printf("奇怪的路径:%s\n", err)
  12. }
  13. return here
  14. }
英文:

This is the traditional way of doing it which I think will work under any platform.

  1. import (
  2. "fmt"
  3. "os"
  4. "path/filepath"
  5. )
  6. // Shortcut to get the path to the current executable
  7. func ExecPath() string {
  8. var here = os.Args[0]
  9. here, err := filepath.Abs(here)
  10. if err != nil {
  11. fmt.Printf("Weird path: %s\n", err)
  12. }
  13. return here
  14. }

答案2

得分: 0

package main

// #import <mach-o/dyld.h>
import "C"

import (
"fmt"
)

func NSGetExecutablePath() string {
var buflen C.uint32_t = 1024
buf := make([]C.char, buflen)

  1. ret := C._NSGetExecutablePath(&buf[0], &buflen)
  2. if ret == -1 {
  3. buf = make([]C.char, buflen)
  4. C._NSGetExecutablePath(&buf[0], &buflen)
  5. }
  6. return C.GoStringN(&buf[0], C.int(buflen))

}

func main() {
fmt.Println(NSGetExecutablePath())
}

英文:

I don't think there is a cross-platform way to do this.

However, on OS X, there is a better way. dyld provides a function _NSGetExecutablePath() that gives you the path to the executable. You can call this with CGo.

  1. package main
  2. // #import &lt;mach-o/dyld.h&gt;
  3. import &quot;C&quot;
  4. import (
  5. &quot;fmt&quot;
  6. )
  7. func NSGetExecutablePath() string {
  8. var buflen C.uint32_t = 1024
  9. buf := make([]C.char, buflen)
  10. ret := C._NSGetExecutablePath(&amp;buf[0], &amp;buflen)
  11. if ret == -1 {
  12. buf = make([]C.char, buflen)
  13. C._NSGetExecutablePath(&amp;buf[0], &amp;buflen)
  14. }
  15. return C.GoStringN(&amp;buf[0], C.int(buflen))
  16. }
  17. func main() {
  18. fmt.Println(NSGetExecutablePath())
  19. }

huangapple
  • 本文由 发表于 2013年4月19日 11:46:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/16096885.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定