VSCode: Could not import Golang package

Jimi picture Jimi · Oct 23, 2019 · Viewed 78.5k times · Source

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?

Answer

Auyer picture Auyer · Oct 23, 2019

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.

.  <- editor open here
| 
|_Folder
  | main.go
  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