Some Go text editors and IDEs (in my particular case, JetBrains's GoLand IDE) will automatically delete unused imports on save. This is ordinarily a good thing, because the Go compiler throws errors for unused imports.
However, in the case of the github.com/fxamacker/cbor/v2
import, I believe GoLand gets confused by the v2
at the end, and thinks the import is never used, because there is never any v2.<something>
in the file anywhere. So GoLand deletes this import when I save, but then my file fails to compile, because the import I need is gone.
I thought I could fix this with a leading underscore, like this:
import (
// ... other imports here ...
_ "github.com/fxamacker/cbor/v2"
)
When I make this change, then GoLand does not delete my import, but then I get errors when I compile:
redacted.go:15:10: undefined: cbor redacted.go:19:13: undefined: cbor redacted.go:20:17: undefined: cbor redacted.go:109:8: undefined: cbor
How can I import this module so that the import isn't auto-deleted by GoLand, and the file can still be compiled?