Go local module import can't be resolved by IDE linter

2.6k views Asked by At

The code compiles fine, but when using the replace directive for mapping package name to a local directory, the Go linter can't pick this up.

I've tried this on both VSCode & Goland, both has the lint error as shown below. The image is extra information, all code & error messages are shown below in text format.

enter image description here

Repository: https://github.com/webberwang/go-lint-error

This is the folder structure:

/core
  /calc
    math.go
  go.mod
/main
  app.go
  go.mod
// main/app.go

package main

import (
    "fmt"
    "github.com/codelabstudios/core/calc" <- ERROR "Cannot resolve directory 'core'"
)

func main() {
    result := calc.Add(1, 2) <- ERROR "Unresolved reference 'Add'"
    fmt.Println("calc.Add(1, 2) => ", result)
}
// main/go.mod

module github.com/codelabstudios/main

go 1.14

require github.com/codelabstudios/core v0.0.0-00010101000000-000000000000

replace github.com/codelabstudios/core => ../core
// core/calc/math.go

package main

import (
    "fmt"
    "github.com/codelabstudios/core/calc"
)

func main() {
    result := calc.Add(1, 2)
    fmt.Println("calc.Add(1, 2) => ", result)
}
// core/calc/go.mod

module github.com/codelabstudios/core

go 1.14
1

There are 1 answers

0
Webber On BEST ANSWER

After some digging, I found out that the "replace" directive is part of the Vgo proposal (the V stands for versioning). This was merged with Go in 1.11.

To fix the local module import error, we just need to enable "Vgo Integration" in the IDE.

enter image description here