Golang gc: Difference between revisions
From wikinotes
(→Basics) |
m (Will moved page Golang compilation to Golang gc without leaving a redirect) |
||
(10 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
This page documents the builtin golang compilation tools.<br> | This page documents the builtin golang compilation tools.<br> | ||
go's build/install/clean aim to replace | go's build/install/clean aim to replace (or dramatically simplify) a [[make|Makefile]]. | ||
= Locations = | |||
<blockquote> | |||
{| class="wikitable" | |||
|- | |||
| <code>~/go/bin/</code> || default <code>$GOBIN</code>, where executables installed | |||
|- | |||
|} | |||
</blockquote><!-- Locations --> | |||
= run = | = run = | ||
Line 33: | Line 42: | ||
go build -a # force rebuild executable, even when up to date | go build -a # force rebuild executable, even when up to date | ||
go build -o altname # build executable as 'altname' | go build -o altname # build executable as 'altname' | ||
</syntaxhighlight> | |||
go clean | <code>go clean</code> and <code>go install</code> are used similar to a makefile for cleaning builds, and installing to host os (see below). | ||
</ | |||
</blockquote><!-- Basics --> | </blockquote><!-- Basics --> | ||
Line 47: | Line 54: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
</blockquote><!-- cross-compilation --> | </blockquote><!-- cross-compilation --> | ||
== Tools == | |||
<blockquote> | |||
<syntaxhighlight lang="bash"> | |||
go build \ | |||
-race `# detect race-conditions` \ | |||
</syntaxhighlight> | |||
</blockquote><!-- Tools --> | |||
</blockquote><!-- build --> | </blockquote><!-- build --> | ||
= clean = | = clean = | ||
<blockquote> | <blockquote> | ||
<syntaxhighlight lang="bash"> | |||
go clean # removes build | |||
go clean -i # removes 'go install'ed executable | |||
go clean -x # print commands as deletes | |||
</syntaxhighlight> | |||
</blockquote><!-- clean --> | </blockquote><!-- clean --> | ||
= install = | = install = | ||
<blockquote> | <blockquote> | ||
When executed without a version, <code>go install</code> will install your executable.<br> | |||
Otherwise, the go index will be consulted for your go binary. | |||
<syntaxhighlight lang="bash"> | |||
go install # install package at 'cwd's executable to $GOBIN | |||
go install example.com/x/foobar@latest # download/install package from go index | |||
</syntaxhighlight> | |||
</blockquote><!-- install --> | </blockquote><!-- install --> |
Latest revision as of 17:03, 26 June 2022
This page documents the builtin golang compilation tools.
go's build/install/clean aim to replace (or dramatically simplify) a Makefile.
Locations
~/go/bin/
default $GOBIN
, where executables installed
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 packageIt 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 executablefoo
)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
andgo install
are used similar to a makefile for cleaning builds, and installing to host os (see below).Cross Compilation
GOOS=darwin GOARCH=386 go build # build for macos GOOS=windows GOARCH=386 go build # build for windowsTools
go build \ -race `# detect race-conditions` \
clean
go clean # removes build go clean -i # removes 'go install'ed executable go clean -x # print commands as deletes
install
When executed without a version,
go install
will install your executable.
Otherwise, the go index will be consulted for your go binary.go install # install package at 'cwd's executable to $GOBIN go install example.com/x/foobar@latest # download/install package from go index