Simple Build

Now that you have your program ready after testing it with go run, you want to build it as a single program for future use. This guide will show you how to do a simple Go Build.

Know Your Output Location

First you need to know where is your output location. Otherwise, Go Build will save it in your current directory. Example, if I want to save it to my current directory bin folder, I recognize the output path as:

./bin

Build It

With that, you're ready. Go ahead and perform the build using the following command:

$ go build ./cmd/YourApp/main.go

or if you want to specify an output path

$ go build -o ./bin/myProgram ./cmd/YourApp/main.go

Test Run The Program

Once done, try to run that program. You should be able to use like just like go run. On Linux, the command is something like:

$ ./path/to/bin/myProgram
Hello World!
$

Did You Know?

  1. Did you know that Go Build only build against your current system's operating system and architecture?
    • Yes, Go Build allows you build binary program to other operating system and architecture. That's called cross-compiling. Otherwise, your current simple build is only usable across the same CPU and same Operating System.

Wrapping Up

That's all about the simple Go Build method. Remember, it can only simply compile to your existing CPU and operating system.