Go Project not recognizing one import but all others work fine

The projects runs and I see not errors. However, I am trying to use "github.com/kelseyhightower/envconfig" but the import is always removed.

I have done the following.

go get "github.com/kelseyhightower/envconfig"

I now see it but it is greyed out compared to the others

go 1.20

require (
    github.com/alexedwards/flow v0.0.0-20220806114457-cf11be9e0e03
    github.com/fatih/color v1.15.0
    golang.org/x/exp v0.0.0-20230321023759-10a507213a29

require (
    github.com/kelseyhightower/envconfig v1.4.0 // indirect
    github.com/mattn/go-colorable v0.1.13 // indirect
    github.com/mattn/go-isatty v0.0.17 // indirect
    golang.org/x/sys v0.6.0 // indirect

Ran go mod download

Try to add the import back to where I need it

package config

import (

    "github.com/kelseyhightower/envconfig"  <-- gets removed

type OAuth struct {
    Key          string    `envconfig:"APP_KEY" default:""`
    Secret       string    `envconfig:"APP_SECRET" default:""`

same thing happens, so I run go mod tidy and now my import is gone in my go.mod file.

I refresh and re-index my project, do the same process again with the same result. I deleted the go.mod file, reinitialized with no avail.

Any other ideas?

>Solution :

You are only using envconfig as a tag for marshal/unmarshal. So it will be removed as it’s not used. You need to USE envconfig in your codes.
For example

type Foo struct{
     Bar string `envconfig:"bar"`
var data []byte // some marshalled data
var foo Foo
envconfig.Unmarshall(&foo,data) // not sure if this is the correct syntax, but you should get the idea

and now since envconfig is being used, it will no longer be removed from your imports.

Leave a Reply