Go
Project
Build and Run
go run .
- build (if necessary) a temporary executable and run the project in the CWDgo build
- build a runnable executable in the CWD
Cross-Compiling
- Set
GOOS
andGOARCH
GOOS=linux GOARCH=amd64 go build
GOOS=darwin GOARDCH=amd64 go build
- More info on the combinations
Modules
go mod init mymod
- create a Go module in the CWD, namedmymod
- Use a module in a code
import
statement, then rungo get .
to pull it intogo.mod
Formatting
go fmt
- format code in the Go stylegoimports -l -w .
- clean up imports for go files in CWD- Install with
go install golang.org/x/tools/cmd/goimports@latest
- Install with
Linting and Vetting
go install golang.org/x/lint/golint@latest
- install the linter.golint ./...
- lint project
Vet can be used to find logical errors.
go vet ./...
All-In-One - golangci-lint
Source: https://github.com/golangci/golangci-lint
- Mac:
brew install golangci-lint
golangci-lint run
- run the suite
Packaging
go install github.com/rakyll/hey@latest
- Download, build and install a given package.