The error message you're encountering suggests that the Go executable is not recognized by the terminal. This typically happens when Go is not installed correctly or its binary path is not added to your system's PATH environment variable.
Here's how to resolve this issue:
Step 1: Install Go
If you haven't installed Go:
- Download Go: Go to the Go download page and download the installer for your operating system.
- Install Go: Follow the instructions to install Go on your system.
Step 2: Verify Go Installation
After installing Go, you need to verify that it is correctly installed and that the go command is accessible from your terminal.
On Windows:
-
Open PowerShell or Command Prompt.
-
Type the following command and press Enter:
go version
You should see output similar to:
go version go1.XX.XX windows/amd64
On macOS/Linux:
-
Open Terminal.
-
Type the following command and press Enter:
go version
You should see output similar to:
go version go1.XX.XX darwin/amd64
If the go version command is not recognized, Go might not be installed correctly, or the Go binary path might not be added to your PATH environment variable.
Step 3: Add Go to PATH
Ensure that your PATH environment variable includes the directory where the Go executables are installed.
Windows:
- Open the Start menu and type "Environment Variables," then select "Edit the system environment variables."
- In the System Properties window, click on the "Environment Variables" button.
- In the Environment Variables window, find the
Path variable under "System variables," and click "Edit."
- Click "New" and add the path to your Go bin directory. The default install location is usually
C:\Go\bin.
- Click OK to close all dialog boxes.
- Open a new PowerShell or Command Prompt and type
go version to verify the installation.
macOS/Linux:
-
Open Terminal.
-
Edit your shell profile file (e.g., .bashrc, .bash_profile, .zshrc, etc.). Add the following lines:
export PATH=$PATH:/usr/local/go/bin
Replace /usr/local/go/bin with the actual path where Go is installed, if it's different.
-
Save the file and source it to apply the changes:
source ~/.bashrc # or source ~/.bash_profile or source ~/.zshrc
-
Verify the installation by typing go version in the terminal.
Step 4: Restart VSCode
After updating your PATH, make sure to restart Visual Studio Code to ensure it picks up the changes to your environment variables.
Additional Configuration in VSCode
- Check Go Extension Settings:
- Press
Ctrl+, to open the settings.
- Search for
Go: Gopath and ensure it points to the correct Go workspace (it usually should be left empty if you're using Go modules).
- Also, search for
Go: GOROOT and ensure it points to the correct installation path of Go (again, it's usually auto-detected and should not need manual configuration).
By following these steps, you should be able to resolve the issue where the go command is not recognized and successfully run Go code in Visual Studio Code.