Go Tools — env, fmt, imports
Continuing from the previous article about introduction to Go Tools ,
In this article, I would like to talk about other Go Tools.
If you want to check Go related environment variables, simply run
$ go env
then, you can list up all the variables.
➜ ~ go env
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/yeongseokkim/Library/Caches/go-build"
GOENV="/Users/yeongseokkim/Library/Application Support/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOINSECURE=""
GOMODCACHE="/Users/yeongseokkim/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="darwin"
GOPATH="/Users/yeongseokkim/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/Cellar/go/1.18.4/libexec"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/Cellar/go/1.18.4/libexec/pkg/tool/darwin_amd64"
GOVCS=""
GOVERSION="go1.18.4"
GCCGO="gccgo"
GOAMD64="v1"
AR="ar"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD="/dev/null"
GOWORK=""
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -arch x86_64 -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/2c/k5j_pm597pqdjwvj8ytvr74h0000gn/T/go-build3346700416=/tmp/go-build -gno-record-gcc-switches -fno-common"
next up, “go fmt or goimports”.
“go fmt” does fomatting given source codes like cleaning whitespaces, fix wrong indentations and etc. and “goimports” updates your Go import lines, adding missing ones and removing unreferenced ones. You can take advantage of “go fmt” by using “goimports”.
For example, if you got this wrongly indented codes, you can simply run the command
$ go fmt main.go
then the outcome is
As the “go fmt” tool automatically formatted the code.
“goimports” also does the same work.
If code looks weird like missing import line and wrong indentation, “goimports” can make it better automatically.
$ goimports -w cmd/demo3/main.go
this is the outcome of executing command above using “goimports”
but do we really need to run those commands on single code write?
To be honest, if you are using VSCode for Go development. Go Extension program handles formatting, automatic organization of imports out of the box.
and other IDE also has the similar kinds of supports for formatting, auto importing, and stuff like that. So, don’t worry.