How do I troubleshoot a "can't find import" error with go/importer when importing gin in Go modules?

62 Views Asked by At

I'm using go/importer and it's default importer to try import gin using this method. I'm using go.mod and I have gin installed on there - go mod tidy changes nothing. Yet when I run this code, I'm getting the error, as seen below. I have the module installed, setup $GOPATH correctly (using Goland IDE) and the web server runs fine. I'm not sure what's going wrong, has anyone seen anything like this?

The code should run a web server, and log the imported package using spew into the console. Yet this is the result we are getting.

package main

import (
    "github.com/davecgh/go-spew/spew"
    "github.com/gin-gonic/gin"
    "go/importer"
    "net/http"
)

func main() {
    r := gin.Default()

    r.GET("/inline", func(context *gin.Context) {
        context.String(http.StatusOK, "inline")
    })

    defaultImporter := importer.Default()

    pkg, err := defaultImporter.Import("github.com/gin-gonic/gin")

    spew.Dump(pkg, err)

    err = r.Run(":8000")
    if err != nil {
        panic(err)
    }
}

And the console output is:

[GIN-debug] [WARNING] Creating an Engine instance with the Logger and Recovery middleware already attached.

[GIN-debug] [WARNING] Running in "debug" mode. Switch to "release" mode in production.
 - using env:   export GIN_MODE=release
 - using code:  gin.SetMode(gin.ReleaseMode)

[GIN-debug] GET    /inline                   --> main.main.func1 (3 handlers)
(*types.Package)(<nil>)
(*errors.errorString)(0xc00038fd90)(can't find import: "github.com/gin-gonic/gin")
[GIN-debug] [WARNING] You trusted all proxies, this is NOT safe. We recommend you to set a value.
Please check https://pkg.go.dev/github.com/gin-gonic/gin#readme-don-t-trust-all-proxies for details.
[GIN-debug] Listening and serving HTTP on :8000
0

There are 0 best solutions below