Go Commands
Go useful commands explained
Here are a list of daily Go commands used by developers, which will help you stay productive if you know about them.
go get
go getDownloads third party packages, by git cloning the repository and installing binaries if any
Example:
# downloads the "errors" package
go get github.com/pkg/errors
o
# downloads and updates the "errors" package
go get -u github.com/pkg/errors
# downloads all packages that the current project imports/uses
go get ./...
# downloads and updates all packages that the current project imports/uses
go get -u ./...go run
go runCompiles and builds the project, after which it generates a binary in a temporary location then executes that binary
Example:
# Compiles "main.go" and executes the binary
go run main.go
# Compiles "main.go" and "package.go" and executes the binary
go run main.go package.go
# Compiles all ".go" files in CWD and executes the binary
go run *.gogo build
go buildCompiles and creates executable binary from source code
Example:
go build
go build -o executable-namego test
go testRuns all Go tests which are located inside _test.go files from current working directory
Example:
go test ./...
go test -shuffle
go test -random
go test -run
go test -vgo install
go installCompiles Go code from binary.go and creates a binary called "bin" then places it inside $GOPATH/bin directory
In order for this to work the file need to in package main and have a main function
Example:
go install bin.gogo fmt
go fmtFormats all Go code from current working directory
Example:
go fmt ./...go vet
go vetChecks for potential Go issues in current working directory
Example:
go vet ./...There’s an enhanced version of go fmt available called goimports that also cleans up your import statements. It puts them in alphabetical order, removes unused imports, and attempts to guess any unspecified imports.
goimports -l -w .The -l flag tells goimports to print the files with incorrect formatting to the console. The -w flag tells goimports to modify the files in-place. The . specifies the files to be scanned: everything in the current directory and all of its subdirectories.
Rather than use separate tools, you can run multiple tools together with golangci-lint. It combines golint, govet, and an ever-increasing set of other code quality tools. Once it is installed, you run golangci-lint with the command:
golangci-lint rungo env
go envDisplays all environment variables that Go uses and their values
go help
go helpDisplays a list of all available Go commands. Have fun and play around with them
For more info about Go commands check out Go Commands
Last updated
Was this helpful?