GoLang’s go build command

Mayur Wadekar
3 min readJan 25, 2020

--

As per stackoverflow survey google’s golang is the popular language for programming. It’s syntax simplicity and inbuild concurrency is best feature golang having (as per my consideration).

Golang offering multiple inbuild tools for developers to use its simplicity and robustness without having more headaches. Some of them are like gofmt(golang code formatter), goimports(adding missing ones and removing unreferenced ones) etc.

To check the existing commands you can it with using following commands

You can check by just typing go or go help

> go
or
> go help

After clicking enter you able to see many commands, same as below

Go Commands List and Description

go build command is generally used to compile the packages and dependencies that you have defined/used in your project.

So how go build is executing internally, what compiler executes, which directories created or deleted; Those all questions are answered by go build command flags. Let’s see some of those.

If you are specifying a single file as an argument to go build then it treats as a single package but if arguments are a list of .go files then all source specifying a single package.

When compiling packages, build ignores files that end in ‘_test.go’.

Some of the popular commands that used when building the package like

go build -x sourcefile.go

It prints all the commands which are go compiler executes while building a binary file.

go build -a sourcefile.go

It forces rebuilding of packages.

go build -n sourcefile.go

It helps to understand what commands will be execute while building a binary. (This only shows commands and not execute it).

go build -work sourcefile.go

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

go build -ldflags=”-flag” sourcefile.go

This flag is related to the linker. ld stands linker. This flag is the overall used flag as it inserts dynamic information at build time in your binary file. What does it mean, So we have sample example here.

I have a directory structure for Cron job server. Which shown in the below frame. It resides in your go workspace as /src/reportCron/

Directory Structure

Simple code with main.go file and here I have defined mainpackagevariable. So we assign value to that variable runtime.

So we have package cronjobs and this package also has its own variable called otherpackagevariable.

So after workspace settlement actual build command in the picture. So many developers do common mistakes that they have given only package name while executing command. As like

go build -ldflags=”-X ‘main.mainpackagevariable=main variable’ -X ‘cronjobs.otherpackagevariable=Other package variable’” main.go

Above syntax compile the build successfully but you will come to know that your otherpackagevariable is assigning to a blank string. So how to compile those nonmain package variables, This is a big question in developers’ mind.

To solve those types of mistakes you have to include path like the whole import path of the package that you’ve used for dynamic linking.

go build -ldflags=”-X ‘main.mainpackagevariable=main variable’ -X ‘Go_Exercise/reportCron/cronjobs.otherpackagevariable=Other package variable’ “ main.go

So this linking gives you a proper output by setting value to your package variable.

So adding those paths will result in a proper output that you are required for.

You can download the source code from my Github page.

Go having kinda cool stuff itself within. So explore more command flags specified in go build.

Hope you’ve enjoyed reading :)

--

--