Go language frequently used command notes

Global install go program(go version >=1.16.0)

when go version is higher 1.12 should install godoc command
  • go get golang.org/x/tools/cmd/godoc
launch go docs services in local
  • godoc -http=:6060
view go environment variables,inlude GO ROOT path
  • go env
fmt package Println docs
  • go doc fmt Println
initialization go modules, when reference sub directory straightway example.com/module/sub1/sub2
  • go mod init example.com/module

delete packages

  • go clean -i -v -x github.com/somepkg/go/simpleGitHub

Add modules to current packages

when use module in local packages.
  • go mod edit -replace github.com/acme/bar=/path/to/local/bar
  • go mod tidy
reformat codes in current directory
  • go fmt
compile specific os binary program
  • CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -ldflags '-extldflags "-static"' -o app .
smallest docker images for go program
1    FROM scratch
2    WORKDIR /app
3    COPY ./app .
4    ENTRYPOINT ["./app"]
other practice
  • decrement size when build binary: go build -ldflags='-s -w'
  • check the override variables: go tool vet main.go
  • compile different platform and architecture
1go tool dist list -json
2GOOS=darwin GOARCH=386 go build main.go