Please disable your ad-blocker to access the content


The Go Compiler

Home » Programming Language » Golang » The Go Compiler
the go compiler

The Go compiler is executed with the help of the go tool, which does many more things than just generating executable files. The command I’m going to present in this article will work on every valid Go source file.

You can compile a Go source file using the go tool compile command. What you will get is an object file, which is a file with the .o file extension. This is illustraed in the output of the next commands, which were executed on a windows operating machine:

$ go tool compile main.go
$ ls -l main.o
-rw-r--r-- 1 KDSINGH 197609 14300 Oct 22 19:14 main.o
$ file main.o
main.o: current ar archive

An object file is a file that contains object code, which is machine code in relocatable format that, most of the time, is not directly executable. The biggest advantage of the relocatable format is that it requires as low memory as possible during the linking phase.

If you use the -pack command-line flag when executing go tool compile, you will get an archive file instead of an object file:

$ go tool compile -pack main.go
$ ls -l main.a
-rw-r--r-- 1 KDSINGH 197609 14300 Oct 22 19:18 main.a
$ file main.a
main.a: current ar archive

An archive file is a binary file that contains one or more files, and it is mainly used for grouping multiple files into a single file. One of these formats is ar, which is used by Go.

you can list the contents of an .a archive file as follows.

$ ar t main.a

Another truly valuable command-line flag of the go tool compile command that is worth mentioning is -race, which allows you to detect race conditions.

$ go tool compile -S main.go

The preceding command generates lots of output that you might find difficult to understand, which means that Go does a pretty good job of hiding any unnecessary complexities,, unless you ask for them.