Golang gc

From wikinotes
Revision as of 01:48, 19 June 2022 by Will (talk | contribs)

This page documents the builtin golang compilation tools.
go's build/install/clean aim to replace or at least dramatically simplify a [Make Makefile].

run

go run is a convenience tool that builds/runs an executable.

go run .                  # compile package at cwd, and run it's 'main()' method
go run . --help           # cli params for the program can be passed to go run

go run main.go lib.go ... # compile only these files, and run detected 'main()' method
go run some/package       # compile a specific package

It accepts all of the same build-flags as go build,
which includes some validation tools.

go run -race .  # compile/run, and detect race conditions

build

Basics

Go build creates an executable with the name of your package.
If your package includes slashes, it is only the basename.
(ex: package: example.com/x/foo creates executable foo)

go build             # build executable for package at cwd
go build -a          # force rebuild executable, even when up to date
go build -o altname  # build executable as 'altname'

go clean and go install are used similar to a makefile for cleaning builds, and installing to host os.

Cross Compilation

GOOS=darwin GOARCH=386 go build   # build for macos
GOOS=windows GOARCH=386 go build  # build for windows

clean

go clean             # removes build
go clean -i          # removes 'go install'ed executable
go clean -x          # print commands as deletes

install