Search code examples
govisual-studio-codego-packages

VSCode: Could not import Golang package


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?


Solution

  • 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