I get this error:
src/huru/utils/utils.go:6:2: imported and not used: "fmt"
src/huru/utils/utils.go:9:2: imported and not used: "net/http"
when I have these unused imports:
import (
"fmt"
"net/http"
)
<rant>
it turns out this a rather seriously annoying "feature" because some IDEs like VSCode will automatically remove unused imports which is f*cking annoying when you are about type the characters that will use the imports but you hit save first or what not.</rant>
Is there a way to ignore this compile error with a command line option? something like:
go install main --ignore-dumb-errors
The source code for several Go tools (including goimports) is kept in the go.tools repository. To install all of them, run the go get command:
$ go get golang.org/x/tools/cmd/...
Or if you just want to install a specific command (goimports in this case):
$ go get golang.org/x/tools/cmd/goimports
To install these tools, the go get command requires that Git be installed locally.
You must also have a workspace (GOPATH) set up; see How to Write Go Code for the details.
$ goimports -help
usage: goimports [flags] [path ...]
-cpuprofile string
CPU profile output
-d display diffs instead of rewriting files
-e report all errors (not just the first 10 on different lines)
-l list files whose formatting differs from goimport's
-local string
put imports beginning with this string after 3rd-party packages; comma-separated list
-memprofile string
memory profile output
-memrate int
if > 0, sets runtime.MemProfileRate
-srcdir dir
choose imports as if source code is from dir. When operating on a single file, dir may instead be the complete file name.
-trace string
trace profile output
-v verbose logging
-w write result to (source) file instead of stdout
$
Run the goimports
command with flag -w
on your source code. It will fix your imports for you. It's how the Go Playground and IDEs fix imports.
For example,
$ cat imports.go
package main
import (
"net/http"
)
func main() {
fmt.Println("Hello, playground")
}
$ goimports -w imports.go
$ cat imports.go
package main
import "fmt"
func main() {
fmt.Println("Hello, playground")
}
$
You can run it on an entire directory too.