8

This question has been answered here but in my case , this isn't working. It may be because of new VS code version. My Steps:

  1. Installed plugin

enter image description here

  1. In command window ctrl+shift+P , entered Go: Install/Update Tools and installed all 17 packages

  2. But I am getting the error. here is the dir structure:

enter image description here

  1. My go env output from cmd:
D:\Personal\Learning\GoLang\Project-1>go env
set GOARCH=amd64
set GOBIN=
set GOCACHE=C:\Users\rog1\AppData\Local\go-build
set GOEXE=.exe
set GOFLAGS=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOOS=windows
set GOPATH=C:\Go\bin;D:\Personal\Learning\GoLang\Project-1;
set GOPROXY=
set GORACE=
set GOROOT=C:\Go
set GOTMPDIR=
set GOTOOLDIR=C:\Go\pkg\tool\windows_amd64
set GCCGO=gccgo
set CC=gcc
set CXX=g++
set CGO_ENABLED=1
set GOMOD=
set CGO_CFLAGS=-g -O2
set CGO_CPPFLAGS=
set CGO_CXXFLAGS=-g -O2
set CGO_FFLAGS=-g -O2
set CGO_LDFLAGS=-g -O2
set PKG_CONFIG=pkg-config
set GOGCCFLAGS=-m64 -mthreads -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=C:\Users\rog1\AppData\Local\Temp\go-build020393970=/tmp/go-build -gno-record-gcc-switches

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.

At line:1 char:1 + go env

+~~

  • CategoryInfo : ObjectNotFound: (go:String) [], CommandNotFoundException
    • FullyQualifiedErrorId : CommandNotFoundException
  1. My PATH environment variable : C:\Go\bin;

  2. My VS Code setting.json entry:

    "go.autocompleteUnimportedPackages": true,
    "go.gopath": "D:\\Personal\\Learning\\GoLang\\Project-1\\",
    "go.goroot": "C:\\Go\\",

What am I missing ?

User985614
  • 330
  • 1
  • 5
  • 18
  • it's look like it doesn't find the go.exe file at your path. try to open your cmd and type go, see if it's showing the help menu or that command isn't found – Or Yaacov Apr 26 '19 at 21:47
  • @OrYaacov The `go` commands work on `cmd` but not in VS code terminal – User985614 Apr 27 '19 at 05:52
  • 1
    can you try to run that command on the vscode terminal (which is an instance of powershell) : $env:Path = [System.Environment]::GetEnvironmentVariable("Path","Machine") – Or Yaacov Apr 27 '19 at 08:29
  • 1
    @OrYaacov : It worked. Super awesome. What's this magical command ? Why its not there anywhere in the tutorial videos ?? :O . PLease put it as an answer so that I can mark it – User985614 Apr 28 '19 at 06:22

2 Answers2

34

please try to run the following command in the vscode terminal (which is an instance of powershell):

 $env:Path = [System.Environment]::GetEnvironmentVariable("Path","Machine") 

It will make the current powershell instance to load the latest PATH variable from the machine.

Or Yaacov
  • 3,597
  • 5
  • 25
  • 49
  • 1
    In the top right of the terminal in vs code, you change the shell type. You can choose cmd instead of Powershell. If your gopath is already configured for cmd, this would resolve the issue. – Rochak Agrawal Jan 27 '20 at 21:40
1

Sometimes it happens when you installed Go and Visual studio code and tried to run the code without restarting the OS.

Resolution
----------------

  • Try restarting the OS.
  • Open Visual studio code and try running the code again.
Sarath Subramanian
  • 20,027
  • 11
  • 82
  • 86