Home > Mobile >  Vscode gives error "could not import fmt (cannot find package "fmt" )" for simpl
Vscode gives error "could not import fmt (cannot find package "fmt" )" for simpl

Time:11-13

Following is my system configuration.

OS : Microsoft Windows 10 Enterprise

Go version: go version go1.15.7 windows/amd64

The program which I am trying to execute is as below

package main

import "fmt"

func main() {
    fmt.Println("Hello World from Go")
}

The problem is I can compile and run a simple Hello world program from the command line but inside vscode it showed the error.

could not import fmt (cannot find package "fmt" in any of   c:\go\src\fmt (from $GOROOT)
    C:\Users\ameena\go\src\fmt (from $GOPATH))compiler

Am I missing here something ??

Regards Amit

CodePudding user response:

As explained here (for GoLand, but it applies to VSCode-Go as well)

GOROOT is a variable that defines where your Go SDK is located.
You do not need to change this variable, unless you plan to use different Go versions.

GOPATH is a variable that defines the root of your workspace.
By default, the workspace directory is a directory that is named go within your user home directory (~/go for Linux and MacOS, %USERPROFILE%/go for Windows).

If you did not install Go in the default paths (C:\Go), you need to set the GOROOT environment variable to the root folder of your Go installation.

The OP Amit Meena confirms in the comments:

Seems in my env GOROOT variable was missing: on creating the GOROOT variable and pointing it to go installation directory, the error has gone from VSCode.

  • Related