Unable to import More packages

Hi Team,

I am not able to import more packages.After saving go file,only one package is showing and other imported packages are gone.Is there any solution for this.I am using Microsoft visual code.

Thanks,
Rajapandian.B

You are probably using goimports (by default, behind the scenes) to rewrite imports on save. It helpfully adds imports you lack and removes imports you don’t need. So, don’t add an import - add an import and something that uses it, or just something that uses it and the import will be added automatically.

2 Likes

Thanks Jakob

Also note that

It is an error to import a package […] without using it.

There are cases where you want to import a package without directly using it. An example for this is a library that implements database/sql. If you want to use SQLite you would do something like this:

import (
    "database/sql"

    _ "github.com/mattn/go-sqlite3"
)

This will make everything provided by the sql package available. The blank indentifier _ used for the SQLite package allows you to import this package without using it directly.

To silence complaints about the unused imports, use a blank identifier to refer to a symbol from the imported package.

Quotes from Effective Go - The Go Programming Language

2 Likes

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.