Search code examples
govisual-studio-codegoimports

How to force VSCode to require manual choice where goimports match multiple packages with same name?


I just ran into issues where I am writing small web service with Go Iris framework. Iris has few useful packages for HTTP request error handling.

Namely, I wanted to use "github.com/kataras/iris/middleware/logger" and "github.com/kataras/iris/middleware/recover" however when I start typing app.Use(logger....) VSCode auto imported "github.com/hashicorp/consul/logger" which is also in my path.

Simply copy-pasting right path will solve the issue, but is there a way to force VSCode to make a manual choice where multiple packages names are matched or even fully disable goimports for these cases.

Example below:

package main

import (
    // "github.com/hashicorp/consul/logger" ! incorrect package

    "github.com/kataras/iris"
    "github.com/kataras/iris/middleware/logger"
    "github.com/kataras/iris/middleware/recover"
)

func main() {
    app := iris.New()
    app.Logger().SetLevel("debug")
    // Optionally, add 2 built'n handlers
    // that can recover from any http-related error
    // and log the requests in terminal
    app.Use(recover.New())
    app.Use(logger.New())
}

Solution

  • If you start typing in the import () declaration, it will give you suggestions that match. So if you were to type logger it would give you the suggestion of:

    github.com/kataras/iris/middleware/logger
    

    and

    github.com/hashicorp/consul/logger
    

    Just put the "" in the import and start typing the package you want.