17. Using Go Tools
Activity 17.01: Using gofmt, goimport, go vet, and go get to Correct a File
Solution:
- Run
gofmt
against the file to check for any formatting issues and see that they make sense:gofmt main.go
This should output a much neater-looking file, as follows:
- Use the
-w
option ongofmt
to make the changes to the file and save them:gofmt -w main.go
- Check the imports are correct using
goimports
:goimport main.go
- Use
goimports
to fix the import statements in the file:goimports -w main.go
- The final stage is to use
go vet
to check for any issues the compiler might miss. Run it againstmain.go
to check for any issues:go vet main.go
- It will find an issue with unreachable code, as shown in the following output:
- Correct the issue by moving the
log.Println("completed")
line so that it runs before thereturn
statement:func ExampleHandler(w http.ResponseWriter, r...