在%PATH%中找不到可执行文件 golang。

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

Executable file not found in %PATH% golang

问题

package main

import (
	"bytes"
	"fmt"

	"os/exec"
)

func main() {

	cmd := exec.Command("dir")
	var stdout, stderr bytes.Buffer
	cmd.Stdout = &stdout
	cmd.Stderr = &stderr
	err := cmd.Run()
	if err != nil {
		fmt.Printf("cmd.Run: %s failed: %s\n", err, err)
	}
	outStr, errStr := string(stdout.Bytes()), string(stderr.Bytes())
	if len(errStr) > 1 {
		fmt.Printf("out:\n%s\nerr:\n%s\n", outStr, errStr)
	}

	fmt.Printf(outStr)
}

嗨,每当我尝试使用go运行这个文件时,它显示了这个错误:"cmd.Run: exec: "dir": 在%PATH%中找不到可执行文件失败"。我已经将golang添加到了我的PATH中,但它仍然失败。

英文:
package main

import (
	"bytes"
	"fmt"

	//"log"
	"os/exec"
)

func main() {

	cmd := exec.Command("dir")
	var stdout, stderr bytes.Buffer
	cmd.Stdout = &stdout
	cmd.Stderr = &stderr
	err := cmd.Run()
	if err != nil {
		fmt.Printf("cmd.Run: %s failed: %s\n", err, err)
	}
	outStr, errStr := string(stdout.Bytes()), string(stderr.Bytes())
	if len(errStr) > 1 {
		fmt.Printf("out:\n%s\nerr:\n%s\n", outStr, errStr)
	}

	fmt.Printf(outStr)
}

*Hi guys, whenever I try to run this file with go it shows me This error "cmd.Run: exec: "dir": executable file not found in %PATH% failed:". I have golang in my PATH but it still failed *

答案1

得分: 1

dir不是Windows中的可执行文件,而是命令提示符的内部命令。你需要将dir传递给命令提示符。

你的命令应该是这样的:

cmd.exe /c dir

你可以这样实现:

args := strings.Split("/c dir", " ")
cmd := exec.Command("cmd.exe", args...)

cmd.Dir = filepath.Join("C:", "Windows")
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr

通过这种方式传递命令行参数,strings.Split()会将"/c dir"按照空格分隔成多个子字符串,并返回这些分隔符之间的子字符串的切片。

另外,如果你需要打印特定位置的目录,你可以设置命令的工作目录:

cmd.Dir = filepath.Join("C:", "Windows")

filepath.Join将任意数量的路径元素连接成一个单一的路径,使用操作系统特定的分隔符进行分隔。

将以下包添加到你的文件中:

import (
	"os"
	"path/filepath"
	"strings"
)

要打印结果,你可以将输出和错误连接到标准输出和标准错误:

cmd.Stdout = os.Stdout
cmd.Stderr = &os.Stderr

你的完整代码如下:

package main

import (
	"fmt"
	"os"
	"os/exec"
	"path/filepath"
	"strings"
)

func main() {
	args := strings.Split("/c dir", " ")
	cmd := exec.Command("cmd.exe", args...)

	cmd.Dir = filepath.Join("C:", "Windows")
	cmd.Stdout = os.Stdout
	cmd.Stderr = os.Stderr

	err := cmd.Run()
	if err != nil {
		fmt.Printf("cmd.Run: %s failed: %s\n", err, err)
	}
}
英文:

dir is not an executable file in Windows, rather it is an internal command of Command prompt. You need to pass dir to command prompt.
Your command would look like this:
> cmd.exe /c dir

You can implement it like this:

> args := strings.Split("/c dir"," ")

> cmd := exec.Command("cmd.exe",args...)

Pass your command line arguments like this, strings.Split() will split "/c dir" into all substrings separated by " " and returns a slice of the substrings between those separators.

Also if you need to print dir of a specific location you can set the working directory of the command:

> cmd.Dir = filepath.Join("C:","Windows")

filepath.Join joins any number of path elements into a single path, separating them with an OS specific Separator.

Add the following packages into your file

import (
"os"
"path/filepath"
"strings"
)

To print the result you can connect your output and error to the standard output, and standard error.

cmd.Stdout = os.Stdout
cmd.Stderr = &os.Stderr

Your overall code would be:

package main

import (
	"fmt"
	"os"
	"os/exec"
	"path/filepath"
	"strings"
)

func main() {
	args := strings.Split("/c dir"," ")
    cmd := exec.Command("cmd.exe",args...)

	cmd.Dir = filepath.Join("C:","Windows")
    cmd.Stdout = os.Stdout
    cmd.Stderr = os.Stderr

    err := cmd.Run()
    if err != nil {
        fmt.Printf("cmd.Run: %s failed: %s\n", err, err)
    }

}

huangapple
  • 本文由 发表于 2022年3月28日 00:33:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/71638194.html
匿名

发表评论

匿名网友

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

确定