Avoid Dot Imports
ID |
go.avoid_dot_imports |
Severity |
low |
Remediation Complexity |
trivial |
Remediation Risk |
low |
Remediation Effort |
low |
Resource |
Code Smell |
Language |
Go |
Tags |
code-style, readability |
Description
Reports a dot import (import . "pkg"). A dot import merges every exported identifier of the
imported package directly into the current file’s scope.
Rationale
With a dot import a bare Println(…) could come from the imported package or from a local
declaration — the reader cannot tell, and tooling has a harder time too. Worse, when the imported
package later adds an exported name it can silently shadow or collide with a local one. Qualified
imports keep every name’s origin explicit and make the file robust against upstream changes.
import . "fmt" // FLAW — Println, Sprintf, ... leak into this file's scope
import "fmt" // OK — qualified: fmt.Println(...)
import f "fmt" // OK — explicit alias
Remediation
Remove the dot and use the package qualifier at each call site (fmt.Println(…)). If the
qualifier is too long, give the import a short explicit alias instead.