Home  >  Article  >  Backend Development  >  Why Does \"sudo go run main.go\" Fail with \"exec: go: executable file not found in $PATH\" on Ubuntu 16.04?

Why Does \"sudo go run main.go\" Fail with \"exec: go: executable file not found in $PATH\" on Ubuntu 16.04?

Susan Sarandon
Susan SarandonOriginal
2024-11-17 06:54:03726browse

Why Does

Troubleshooting "sudo go run main.go" Error

When attempting to execute "sudo go run main.go" to capture network packets using "gopacket" on Ubuntu 16.04, you may encounter the error message "exec: go: executable file not found in $PATH." This error arises because the necessary environment variables are not set for the root user.

Solution:

Instead of using "sudo go run ...," follow these steps:

1. Compile the Binary without sudo:

  • Use "go build" to compile the main.go file into an executable binary. For example:

    go build

2. Run the Binary with sudo:

  • Execute the compiled binary with sudo. For instance:

    sudo ./mycapt

Alternatively:

  • Use "go install" to install the binary into your system's $GOPATH/bin directory.

    go install
  • Then run the binary with sudo from the $GOPATH/bin directory:

    sudo $GOPATH/bin/mycapt

By compiling and running the binary separately, you ensure that the necessary environment variables are available to the root user.

The above is the detailed content of Why Does \"sudo go run main.go\" Fail with \"exec: go: executable file not found in $PATH\" on Ubuntu 16.04?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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