Home > Backend Development > Golang > Why Does My Go Code Return 'fork/exec . no such file or directory' When Using `exec.Command`?

Why Does My Go Code Return 'fork/exec . no such file or directory' When Using `exec.Command`?

Patricia Arquette
Release: 2024-12-09 02:52:16
Original
478 people have browsed it

Why Does My Go Code Return

Error: "fork/exec . no such file or directory" in Go with fork/exec

When running the following Go code, you may encounter the error "fork/exec . no such file or directory":

func loop1(gor_name string, ras_ip string) {
    // ...
    c := fmt.Sprintf("%s %s %s %s", "./goreplay  --input-file ", gor_name, " --input-file-loop --output-http ", ras_ip)
    cmd := exec.Command(c)
    // ...
}
Copy after login

This error occurs because the function exec.Command requires the program name as the first argument followed by its arguments. The code is currently specifying the entire command as a single string, leading to the error.

To resolve this issue, modify the code to use the correct function signature:

cmd := exec.Command("./goreplay", "--input-file", gor_name, "--input-file-loop", "--output-http", ras_ip)
Copy after login

In this updated code, the program name "goreplay" and its arguments are passed as separate parameters to exec.Command.

The above is the detailed content of Why Does My Go Code Return 'fork/exec . no such file or directory' When Using `exec.Command`?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template