VSCode: Could not import Golang package

170k Views Asked by At

I'm writing a Go project inside my GoPath, and i'm using the Redigo package for connecting to a Redis Server. The application runs fine, however in VSCode there is this annoying error on package import, which is preventing VSCode from giving intellisense suggestions

Could not import github.com/gomodule/redigo/redis (no package data for import path github.com/gomodule/redigo/redis)

This is my VSCode settings.json

{
    "editor.fontSize": 14,
    "editor.formatOnPaste": true,
    "editor.multiCursorModifier": "ctrlCmd",
    "editor.snippetSuggestions": "top",
    "extensions.ignoreRecommendations": false,
    "workbench.statusBar.visible": true,
    "workbench.iconTheme": "vscode-great-icons",
    "files.autoSave": "afterDelay",
    "go.useLanguageServer": true,
    "go.alternateTools": {
        "go-langserver": "bingo"
    },
    "go.toolsEnvVars": {
        "GO111MODULE": "on"
    },
    "go.languageServerExperimentalFeatures": {
        "autoComplete": true,
        "documentSymbols": true,
        "findReferences": true,
        "format": true,
        "goToDefinition": true,
        "goToTypeDefinition": true,
        "hover": true,
        "signatureHelp": true,
        "rename": true,
        "workspaceSymbols": true,
    },
    "go.lintTool": "golangci-lint",
    "go.lintFlags": [
        "--fast",
        "-E", "goimports",
        "-E", "gocritic",
        "-E", "gocyclo",
        "-E", "gosec",
        "-E", "maligned",
        "-E", "scopelint",
        "-E", "interfacer",
        "-E", "goconst",
        "-E", "unconvert",
        "-E", "unparam",
        "-E", "prealloc",
        "-E", "varcheck",
    ],
    "go.formatTool": "goimports",
    "editor.minimap.enabled": false,
    "breadcrumbs.enabled": false,
    "git.autofetch": true,
    "workbench.startupEditor": "newUntitledFile",
    "explorer.confirmDelete": false,
    "git.enableSmartCommit": true,
    "git.confirmSync": false,
    "window.zoomLevel": 0,
    "explorer.confirmDragAndDrop": false
}

I already have the GO111MODULE env var set to on, this is the output of go env

set GOARCH=amd64
set GOBIN=C:\Users\Francesco\Go\bin
set GOCACHE=C:\Users\Francesco\AppData\Local\go-build
set GOEXE=.exe
set GOFLAGS=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOOS=windows
set GOPATH=C:\Users\Francesco\Go
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=C:\Users\Francesco\Go\src\test\go.mod
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\FRANCE~1\AppData\Local\Temp\go-build928398422=/tmp/go-build -gno-record-gcc-switches

What should I change to make this work?

17

There are 17 best solutions below

10
On BEST ANSWER

This happens to me in a few specific situations. This is my troubleshooting process :

  1. Did you run go get github.com/gomodule/redigo/redis?

  2. Sometimes I have a similar issue when I open my editor in a root different than my project. (also check the updates at the end of the answer)

.  <- editor open here
| 
|_Folder
  | main.go
  | go.mod
  | go.sum
  1. Make sure your tools are up to date: run ctrl + shift + p, type Go and chose Install/Update tools.

  2. Try moving your project out of the GOPATH, and setting up go.mod for it.

  3. Restart the editor

Update for those with the issue "2":

Go 1.18 has a new feature called workspace! If you are opening your workspace in a root different than where your go mod files are, it is probably because you have multiple projects in the same folder. If that is the case, you can run:

go work init
go work use ./path-to-module  ./path-to-module2

What it looks like:

.  <- editor open here
| go.work
| app (folder)
  | go.mod
  | go.sum
  | main.go
| client (folder)
  | go.mod
  | go.sum
  | main.go
0
On

I ran into this issue when I had two files in one package that had different package names. Eg: one file had package main while the other had package app.

Ensuring all files have the same package name will result in imports across the package to start working again.

0
On

I encountered a similar problem, but the situation is slightly different. I encountered this problem in VSCode remote, and even the basic library like net/http cannot be imported. I found that it was because of some syntax errors in other files under the same package, such as undefined variables. When I solved these syntax errors and restarted VSCode, I found that the problem was solved.

0
On

if you are using Go 1.18+, here is a new feature for multi-module workspaces, once you setup the go.work file at you root project folder, the issues will no longer exist, you can check the following links for more details

  1. https://stackoverflow.com/a/59485684/7706503
  2. https://github.com/golang/tools/blob/master/gopls/doc/workspace.md
0
On

I just recently ran into this same error, my solution was ensuring that my go version of my project's mod file was the same version as the installed version on my system.

1
On

I had the same problem it was from go mod init packegeName and a named my package 'time' and the editor give me err so I simply replace it with 'timee' i do not know whats happen actually but it's working fine

0
On

My problem was I had the root folder (My Parent folder) which was the parent of my Go project folder open in VSCode

Ie,

My Parent folder
    My Go Project folder

Instead of opening the parent folder (My Parent folder) you could just open the project folder (My Go Project folder)

In my case, I needed access to files in both folders, so I added both parent & child folders to the VSCode workspace as per here and this got rid of the errors

0
On

I've installed on Fedora 35 gcc-go and after I uninstalled it all worked fine.

sudo dnf remove gcc-go
0
On

Doing a full restart of vsCode fixed the issue for me.

Note cmd + shift + p -> Developer: Reload Window did NOT fix it, only a full close and re-opening of VS code

0
On

I had the same symptoms. In my case, the root cause was the missing "CGO_ENABLED=1". Cause I had some "C" code in the package.

0
On

If this is a repo that you have pulled from Github, and you are faced with the errors of a package/s could not be imported and have a go.mod in the repo, I did the following to fix:

  1. Open terminal and navigate to the root of the project where your go.mod and go.sum are located and run:
go mod download

Which as the go help menu outlines that the command: downloads modules to local cache

  1. Inside VS Code open the command palette with whatever command that you have for your architecture. For mine command on mac I use control + shift + p and search for Go: Install/Update tools. Press enter and update your go tooling.

After that my main.go updated with packages that I was missing without explicitly running go install thingVSCodeSaidIWasMissing

0
On

I'm newbie with Golang, I don't know the consequencies of these commands in bigger project.

In my case, the problem was that I moved the probject to a subfolder. I had to delete go.mod and go.sum, and do go mod init package_name followed by go mod tidy

0
On

This worked for me:

  1. File/New Window
  2. File/Add Folder To Workspace ...
1
On

Setting up your workspace

gopls supports both Go module and GOPATH modes. However, it needs a defined scope in which language features like references, rename, and implementation should operate.

The following options are available for configuring this scope:

Module mode

One module

If you are working with a single module, you can open the module root (the directory containing the go.mod file), a subdirectory within the module, or a parent directory containing the module.

Note: If you open a parent directory containing a module, it must only contain that single module. Otherwise, you are working with multiple modules.

Multiple modules

Gopls has several alternatives for working on multiple modules simultaneously, described below. Starting with Go 1.18, Go workspaces are the preferred solution.

Go workspaces (Go 1.18+)

Starting with Go 1.18, the go command has native support for multi-module workspaces, via go.work files. These files are recognized by gopls starting with [email protected].

The easiest way to work on multiple modules in Go 1.18 and later is therefore to create a go.work file containing the modules you wish to work on, and set your workspace root to the directory containing the go.work file.

For example, suppose this repo is checked out into the $WORK/tools directory. We can work on both golang.org/x/tools and golang.org/x/tools/gopls simultaneously by creating a go.work file:

cd $WORK
go work init
go work use tools tools/gopls

...followed by opening the $WORK directory in our editor.


Source

0
On

go mod init package_name followed by go mod tidy got things working for me. go mod tidy installed the packages and updated the mod file.

0
On

Ran into the same issue, reinstalled go with goenv, and then i ran some go mod whys to check if everything is in order and it was

2
On

I had a same sort of issue with a different package (on mac),

  1. Update the go tools - ctrl + shift + p or cmd + shift + p and update/install go tools
  2. Restart VScode

Issue solved