Skip to content

Instantly share code, notes, and snippets.

@mmcdaris
Last active March 21, 2024 09:59
Show Gist options
  • Star 12 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save mmcdaris/b695e8e1ef8ed690c414 to your computer and use it in GitHub Desktop.
Save mmcdaris/b695e8e1ef8ed690c414 to your computer and use it in GitHub Desktop.
going through the go build command

usage

go build: compiles the packages named by the import paths, along with their dependencies, the binary does not end up in $GOPATH/bin it gets created in the dirs

go build [-o output] [build flags] [packages]

If the [packages] are a list of .go files, build treats them as a list of source files specifying a single package.

  go build [ list of .go source files ]
  go build *.go
  the build command expects to receive a list of go source files

When the command line specifies a single main package, build writes the resulting executable to output. Otherwise build compiles the packages but discards the results, serving only as a check that the packages can be built.

The -o flag specifies the output file name. If not specified, the output file name depends on the arguments and derives from the name of the package, such as p.a for package p, unless p is 'main'. If the package is main and file names are provided, the file name derives from the first file name mentioned, such as f1 for 'go build f1.go f2.go'; with no files provided ('go build'), the output file name is the base name of the containing directory.

The build flags are shared by the build, install, run, and test commands:

-a
	force rebuilding of packages that are already up-to-date.
-n
	print the commands but do not run them.

  1) makes a new directory (_obj) in each of your packages
  - compiles with 6g
  - packs with pack
  - and links with 6l
  - a.out renamed to your dir or -o specification

-p n
	the number of builds that can be run in parallel.
	The default is the number of CPUs available.

  This edits config and provides no error if you surpass your core count

-race
	enable data race detection.
	Supported only on linux/amd64, darwin/amd64 and windows/amd64.
  I wonder how you actually detect a data race after using this flag
  maybe more insight with -n
    with -n it reaveals that this is done with the following libs

      runtime
      runtime/race
      math
      fmt
      reflect
      strconv
      unicode/utf8
      os
      time
      syscall
      io
      sync
      sync/atomic
      runtime/cgo
      runtime/errors
      Then your go source packages!
-v
	print the names of packages as they are compiled.

  Outputs to stderr (stream 2)
  Great parsing potential!
    go build -v
      github.com/mmcdaris/animal
      github.com/mmcdaris/nature
    go build -v -race
      runtime
      errors
      runtime/cgo
      runtime/race
      sync/atomic
      math
      unicode/utf8
      github.com/mmcdaris/animal
      sync
      io
      syscall
      time
      strconv
      os
      reflect
      fmt
      github.com/mmcdaris/nature
    I captured output with these 2 commands:
      go build -v -race 2>&1 | pbcopy

-work
	print the name of the temporary work directory and
	do not delete it when exiting.

  On darwin mine was: **/var/folders/00/zhsr0gb13vgc6fpblgmqkzym0000gp/T/go-build722379835**
  So thats cool I guess :)
  I wonder if there is a case where I would want to rebuild from these work files :|
  there are .a files and stuff for the compiler to play with


-x
	print the commands
  I guess that is print the commends and also run them?
  If I am right then I will end up with a nature binary and output

== Compiler Options ==

-ccflags 'arg list'
	arguments to pass on each 5c, 6c, or 8c compiler invocation.

  see what you can use!:
    /usr/local/homebrew/Cellar/go/1.2.1/libexec/pkg/tool/darwin_amd64/6g -h

-compiler name name of compiler to use, as in runtime.Compiler (gccgo or gc).

  pick from the list:
    ls /usr/local/homebrew/Cellar/go/1.2.1/libexec/pkg/tool/darwin_amd64/

-gccgoflags 'arg list'
	arguments to pass on each gccgo compiler/linker invocation.
-gcflags 'arg list'
	arguments to pass on each 5g, 6g, or 8g compiler invocation.
-installsuffix suffix
	a suffix to use in the name of the package installation directory,
	in order to keep output separate from default builds.
	If using the -race flag, the install suffix is automatically set to race
	or, if set explicitly, has _race appended to it.
-ldflags 'flag list'
	arguments to pass on each 5l, 6l, or 8l linker invocation.
-tags 'tag list'
	a list of build tags to consider satisfied during the build.
	See the documentation for the go/build package for
	more information about build tags.

The list flags accept a space-separated list of strings. To embed spaces in an element in the list, surround it with either single or double quotes.

For more about specifying packages, see 'go help packages'. For more about where packages and binaries are installed, run 'go help gopath'. For more about calling between Go and C/C++, run 'go help c'.

See also: go install, go get, go clean.

@mrprogrammer2938
Copy link

😎


Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment