谷歌的Golang执行退出状态为2和1。

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

google golang exec exit status 2 and 1

问题

我想在Go语言中执行Android SDK平台工具中的dexdump。

我已经设置了PATH变量。(我使用的是Ubuntu 12.04)

这是我的代码:

  1. package main
  2. import (
  3. "bytes"
  4. "fmt"
  5. "log"
  6. "os/exec"
  7. )
  8. func main() {
  9. path, err := exec.LookPath("dexdump")
  10. if err != nil {
  11. log.Fatal(err)
  12. }
  13. fmt.Println(path)
  14. cmd := exec.Command(path)
  15. var out bytes.Buffer
  16. cmd.Stdout = &out
  17. err2 := cmd.Run()
  18. if err2 != nil {
  19. log.Fatal(err2)
  20. }
  21. fmt.Printf("%q\n", out.String())
  22. }

结果:
/home/gunwoo/android-sdk-linux/platform-tools/dexdump

2012/10/15 16:44:39 退出状态 2

退出状态 1

为什么Go找不到路径?

英文:

I want execute the dexdump in Android SDK platform-tools on Go language.

I already set the PATH variable. (I'm use Ubuntu 12.04)

Here is my code:

  1. package main
  2. import (
  3. "bytes"
  4. "fmt"
  5. "log"
  6. "os/exec"
  7. )
  8. func main() {
  9. path, err := exec.LookPath("dexdump")
  10. if err != nil {
  11. log.Fatal(err)
  12. }
  13. fmt.Println(path)
  14. cmd := exec.Command(path)
  15. var out bytes.Buffer
  16. cmd.Stdout = &out
  17. err2 := cmd.Run()
  18. if err2 != nil {
  19. log.Fatal(err2)
  20. }
  21. fmt.Printf("%q\n", out.String())
  22. }

Result:
/home/gunwoo/android-sdk-linux/platform-tools/dexdump

2012/10/15 16:44:39 exit status 2

exit status 1

why go doesn't find the path?

答案1

得分: 6

你没有为exec.Rundexdump命令提供任何参数,这可能会导致出现以下错误:

  1. dexdump: 未指定文件
  2. dexdump: [-f] [-h] dexfile...
  3. -d : 反汇编代码段
  4. -f : 显示文件头的摘要信息
  5. -h : 显示文件头的详细信息
  6. -C : 解码(还原)低级符号名称
  7. -S : 仅计算大小

当你运行以下版本的程序时,你会得到什么输出?

  1. package main
  2. import (
  3. "bytes"
  4. "fmt"
  5. "log"
  6. "os/exec"
  7. )
  8. func main() {
  9. path, err := exec.LookPath("dexdump")
  10. if err != nil {
  11. log.Fatal("LookPath: ", err)
  12. }
  13. fmt.Println(path)
  14. cmd := exec.Command(path)
  15. var out bytes.Buffer
  16. cmd.Stdout = &out
  17. err = cmd.Run()
  18. fmt.Printf("%s\n", out.String())
  19. if err != nil {
  20. log.Fatal("Run: ", err)
  21. }
  22. }
英文:

You don't provide any arguments for the exec.Run dexdump command, which possibly generates an error like:

  1. dexdump: no file specified
  2. dexdump: [-f] [-h] dexfile...
  3. -d : disassemble code sections
  4. -f : display summary information from file header
  5. -h : display file header details
  6. -C : decode (demangle) low-level symbol names
  7. -S : compute sizes only

What output do you get when you run the following version of the program?

  1. package main
  2. import (
  3. "bytes"
  4. "fmt"
  5. "log"
  6. "os/exec"
  7. )
  8. func main() {
  9. path, err := exec.LookPath("dexdump")
  10. if err != nil {
  11. log.Fatal("LookPath: ", err)
  12. }
  13. fmt.Println(path)
  14. cmd := exec.Command(path)
  15. var out bytes.Buffer
  16. cmd.Stdout = &out
  17. err = cmd.Run()
  18. fmt.Printf("%s\n", out.String())
  19. if err != nil {
  20. log.Fatal("Run: ", err)
  21. }
  22. }

huangapple
  • 本文由 发表于 2012年10月15日 15:48:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/12891294.html
匿名

发表评论

匿名网友

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

确定