Go build args, our guide in this quest, Providing the tools to put our code to the test.

With -o we specify, the name of our file, And with -v we see, each step with a smile.

For optimization, we use the flag -ldflags, To ensure speed and efficiency, with each line and tag.

And when dependencies arise, -i is our friend, Installing them with ease, until the end.

-x helps us debug, when things go astray, Displaying each command, with clarity each day.

Together these arguments, help us build with grace, Bringing our code to life, in the production space.

So let us embrace, these build args with pride, For a successful journey, with our code by our side.

What are Go Build Arguments?

Go build arguments are flags or options that can be passed to the go build command to control the build process. They can be used to specify the target architecture, enable or disable optimization, set environment variables, and more. Some of the most commonly used build arguments include:

  • -o: specifies the output file name for the compiled binary
  • -v: displays the verbose output of the build process
  • -i: installs the dependencies required by the project
  • -x: displays the commands executed by the build process

Why Use Go Build Arguments?

Go build arguments can be extremely useful in several different scenarios. For example, they can be used to optimize the build process for different environments, such as production or development. Additionally, they can be used to debug build errors, specify the target architecture, or set environment variables. By using build arguments, developers can easily customize the build process to suit their specific needs.

How to Use Go Build Arguments

To use Go build arguments, simply pass them as flags to the go build command. For example, to build a project with verbose output, you would run the following command:

go build -v myproject

You can also pass multiple build arguments by separating them with a space. For example, to build a project with verbose output and install its dependencies, you would run the following command:

go build -v -i myproject

How would you use the build args for Production?

Well, There are many build args and mostly I'd go with the following build args and especially ldflags

CGO_ENABLED=0 go build -v -ldflags='-s -w -X "main.version=0.0.1"' -tags "osusergo netgo" -trimpath ./myproject

In case if you are wondering, what are those ldflags?

In Go, -ldflags is a build argument that allows you to specify additional linker options when compiling a binary. These options can include values such as the version of your application, the date of compilation, and more. When building for production, it is a good practice to use -ldflags to optimize your binary and make it more efficient.

Here are some common -ldflags options for production:

  1. -s: Strips debug information from the binary, reducing its size.
  2. -w: Disables DWARF debugging information, further reducing the size of the binary.
  3. -X: Allows you to set values for global variables in your code. For example, you can use -X main.version=1.0.0 to specify the version of your application or build Tag where you can specify the docker image tag.
  4. -extldflags: Allows you to pass options to the external linker.

To use -ldflags, simply pass the desired options as an argument to the go build command. For example:

go build -ldflags="-s -w -X main.version=1.0.0" myproject

By using -ldflags in production, you can optimize your binary and improve its performance. It is an important tool in the Go build process, and one that every developer should be familiar with.

Conclusion

Go build arguments provide an easy way to customize the build process and make it more efficient. By understanding the basics of build arguments, developers can improve the build process and produce better, more reliable code. Whether you're a beginner or an experienced Go developer, incorporating build arguments into your workflow can help you take your skills to the next level.