DEV Community

Divyanshu Shekhar
Divyanshu Shekhar

Posted on

Go Run vs Go Build: What’s the Best Tool for Your Project?

As a Go developer, you have likely used the two commonly used commands, go run and go build, which assist in building, testing, and running your Go applications. Both commands serve specific purposes and perform different functions.

What is a Go Build Tool?

Go build is a command-line tool used to compile Go programs. It compiles a Go source code file and creates an executable binary file.

The Go build tool automatically determines the dependencies of the Go package and compiles them as well. The Go build tool can be used for building a single package or an entire project.

Features of Go Build

Features of Go build Some of the notable features of the Go build tool are:

  • Cross-platform compilation: The Go build tool can compile Go programs for different operating systems and architectures.
  • Incremental compilation: The Go build tool only recompiles the files that have changed, which makes the build process faster.
  • Automatic dependency management: The Go build tool automatically detects and manages the dependencies of the Go package.
  • Customization: The Go build tool provides various options to customize the build process, such as specifying the output directory and setting build tags.

Go Build Tool Usage

The basic syntax for the Go build command is:

go build [build flags] [packages]

The “packages” argument is the path to the Go package to be built. If no package is specified, Go will attempt to build the package in the current directory.

The build flags are optional and can be used to customize the build process. Some of the commonly used build flags are:

  • go build -o: specifies the output file name. By default, Go build generates an executable file with the same name as the package in the current directory.
  • go build -a: forces rebuilding of all packages, including those that are already up to date.
  • go build -v: enables verbose output and displays the names of the packages as they are compiled.
  • go build -x: prints the commands as they are executed.
  • go build -race: enables data race detection during the build process.
  • go build -mod: specifies the module download mode.
  • go build -tags: includes or excludes specific pieces of code during the build process.

Building a package in Golang

go build github.com/example/mypackage

This command builds the “mypackage” package located in the “github.com/example” repository.

Go build -o flag

The “-o” flag specifies the output file name. By default, Go build generates an executable file with the same name as the package in the current directory.

go build -o myapp github.com/example/myapp

The “go build -o” command builds the “myapp” package located in the “github.com/example” repository and creates an executable binary file named “myapp” in the current directory.

Learn about Go Run flags and Go Build flags from the original post. Read more.

Top comments (0)