11

After running...

go run main.go

I get this error

go : The term 'go' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify
that the path is correct and try again.
Travis Rivera
  • 428
  • 1
  • 4
  • 19

5 Answers5

10

SOLVED: Go to visual studio code (assuming this is installed) ctrl + shift + p to open command window type

Go: current GOPATH

this should give you a path create a system environment variable named GOPATH and add that path into that variable.

Make sure the system environment variables are set to:

GOPATH is set (in my case) to C:\Users[username]\go

GOROOT is set to C:\go\

IDE is Visual Studio Code.

Travis Rivera
  • 428
  • 1
  • 4
  • 19
  • 2
    On Windows instillation, the Go MSI installs GOPATH as a user variable instead of a system variable. Thanks for the fix. – NicoLA Oct 27 '18 at 19:23
  • I get - Go: : The term 'Go:' is not recognized as the name of a cmdlet, function, script file, or operable program. – ilans Dec 27 '19 at 12:58
  • 3
    Restart VSCode is required. – Avi May 22 '20 at 05:46
9
$env:Path = [System.Environment]::GetEnvironmentVariable("Path","Machine")

write the code to the terminal and run it. Eazy way to solve the problem.

Artyom Vancyan
  • 5,029
  • 3
  • 12
  • 34
Mcan_Cicek
  • 234
  • 2
  • 7
3

I had same issue. Then found the below solution on the book "Learning Go" of O'REILLY.

If you are on a Unix-like system using bash, add the following lines to your .profile. (If you are using zsh, add these lines to .zshrc instead):

export GOPATH=$HOME/go
export PATH=$PATH:$GOPATH/bin

You’ll need to source $HOME/.profile to make these changes take effect in your cur‐ rent terminal window. On Windows, run the following commands at the command prompt:

setx GOPATH %USERPROFILE%\go
setx path "%path%;%USERPROFILE%\bin"

After running these commands, you must close your current command prompt and open a new one for these changes to take effect.

1

A couple of solutions:

  • Run refreshenv to refresh the PATH environment variable in the current terminal session.
PS C:\> refreshenv
Refreshing environment variables from the registry for powershell.exe. Please wait...
Finished
PS C:\> go version
go version go1.18.4 windows/amd64
  • Open a new terminal window and try again.

  • Restart PC and try again.

Serzhan Akhmetov
  • 2,898
  • 2
  • 13
  • 26
-2

I was facing similar issues for commands installed via pip, I had to add the full path to Scripts folder to the PATH variable

C:\Users\username\.pyenv\pyenv-win\versions\3.x.y\Scripts

For go too I believe something similar is to be done.

ishandutta2007
  • 16,676
  • 16
  • 93
  • 129