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

5/5 - (2 votes)

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.

In this blog post, we will dive deeper into the differences between these two commands, and help you choose the right one for your project.

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:

  1. Cross-platform compilation: The Go build tool can compile Go programs for different operating systems and architectures.
  2. Incremental compilation: The Go build tool only recompiles the files that have changed, which makes the build process faster.
  3. Automatic dependency management: The Go build tool automatically detects and manages the dependencies of the Go package.
  4. 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.

Building for a different operating system

GOOS=windows go build github.com/example/myapp

This command builds the “myapp” package located in the “github.com/example” repository for the Windows operating system.

Build a specific file in Go

go build github.com/example/mypackage/myfile.go

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

Go build -tags flag

The go build “-tags” flag can be used to include or exclude specific pieces of code during the build process.

go build -tags production github.com/example/myapp

This command builds the “myapp” package located in the “github.com/example” repository with the build tag “production”.

Go build -gcflags

The go build “-gcflags” option specifies compiler flags to pass to the Go compiler.

The “-N” flag is used to disable optimizations, and the “-l” flag to disable inlining.

go build -gcflags "-N -l" github.com/example/myapp

Go Build for a specific architecture

The “GOARCH” environment variable specifies the target architecture.

GOARCH=arm go build github.com/example/myapp

This command builds the “myapp” package located in the “github.com/example” repository for the ARM architecture.

Go build -v flag

Enabling verbose output is accomplished by using the “-v” flag.

go build -v github.com/example/myapp

The Go command compiles the “myapp” package from the “github.com/example” repository and displays the package names as it compiles them.

Go build -race flag

The go build “-race” flag enables data race detection during the build process.

go build -race github.com/example/myapp

This command builds the “myapp” package located in the “github.com/example” repository with race detection.

Go build -mod flag

The go build “-mod” option specifies the module download mode.

go build -mod=vendor github.com/example/myapp

This command builds the “myapp” package located in the “github.com/example” repository using the vendor directory as the module cache.

Run Go Build Executable

Once you have built your Go program with the Go build tool, you can run the resulting executable file on your machine. Running the executable allows you to test and verify the functionality of your program.

To run a Go build executable, navigate to the directory where the executable file is located using the command line. You can then execute the file by typing its name and pressing Enter.

An example of how to run a Go build executable named “myapp.exe” on a Windows machine:

$ cd C:\path\to\executable\directory
$ myapp.exe

If you need to run the executable on a different operating system, such as Linux or MacOS, you must use a compatible environment or a tool such as Wine to emulate the target platform.

What is the Go run Tool?

The Go run tool is a built-in command-line utility that allows you to compile and run Go code quickly and easily. It’s part of the Go programming language distribution and can be accessed from the command line. The tool is particularly useful for small-scale programs that don’t require a full build system or complex configuration.

Features of Go Run

The Go run tool is particularly useful for running small-scale programs that don’t require a full build system or complex configuration.

Some of the most common features of the Go run tool are:

  1. Compiling Go Code

The Go run tool is primarily used to compile and execute Go code. When you run a Go file using the Go run tool, it will automatically compile the code and execute it for you. This saves you the trouble of manually compiling and running the code.

  1. Running Unit Tests

The Go run tool also allows you to run unit tests for your Go code. This is particularly useful when you’re developing a large-scale application and want to test individual components of the code. To run unit tests using the Go run tool, you can use the “go test” command followed by the name of the file you want to test.

  1. Cross-Platform Compilation

The Go run tool also allows you to compile your code for different platforms. This is particularly useful when you’re developing a cross-platform application and need to ensure that your code works on different operating systems. To compile your code for a different platform, you can use the “GOOS” and “GOARCH” environment variables.

Go run Usage

The syntax for using the Go run tool is simple. You can use the “go run” command followed by the name of the file you want to run.

For example:

go run main.go

Here, we’re running a program called “main.go”.

Go run flags

The Go run tool also allows you to specify various parameters and options that can modify its behavior. Some of the most common parameters are:

  • -a: Force rebuilding of packages that are already up-to-date.
  • -n: Print the commands that would be executed but don’t run them.
  • -p n: Set the number of CPUs to use during the build process.
  • -race: Enable data race detection.
  • -v: Print the name of the packages as they are compiled.

Go Run Force Rebuild

The go run “-a” flag is used to force rebuilding of packages that are already up-to-date.

You may use the flag when you suspect that Go is not compiling your code correctly or when you want to ensure that all packages are up-to-date after making changes to your source code.

go run -a main.go

Go Run Print Commands

The go run “-n” flag is used to print the commands that would be executed, but without actually running them.

This is useful when you want to see what Go run would do, but you don’t want to execute the commands.

go run -n main.go

Go Run With Number of CPUs

The go run “-p” flag is used to set the number of CPUs to use during the build process.

This is useful when you want to speed up the build process by using multiple CPUs.

go run -p 4 main.go

In this example, we’re setting the number of CPUs to 4. This will use four CPUs during the build process.

Go run -race flag

The go run “-race” flag is used to enable data race detection. This is useful when you want to detect race conditions in your code.

go run -race main.go

In this example, we’re enabling data race detection for the file main.go.

Go Run Print Package Name

The go run “-v” flag is used to print the name of the packages as they are compiled. This is useful when you want to see which packages are being compiled.

go run -v main.go

In this example, we’re printing the name of the package as it’s compiled for the file main.go.

Take a look at the more Go Command line tools.

Differences between Go Run and Go Build

Now that we have understood the basic functionalities of both Go run and Go build, let’s take a look at some of the key differences between the two.

  1. Compilation and Execution

Go run performs a single-step process of compiling and executing the code, while Go build separates the compilation and execution into two distinct steps. Go run enables you to test your code rapidly without the need for creating an executable file. In contrast, Go build generates an executable file that can run on any platform without requiring Go installation.

  1. Performance

Since Go run compiles and executes the code in a single step, it may take longer to execute than Go build. This is because Go build compiles the code and optimizes it for better performance. However, the difference in performance may not be noticeable for small programs or code snippets.

  1. Debugging

Go run is useful for testing and debugging small programs or code snippets. However, it may not be practical to use Go run for debugging larger projects. Instead, developers can use Go build, which creates an executable file that they can use to debug and profile the application actively.

  1. Configuration

Go run does not provide any options for configuring the build process. On the other hand, Go build provides several options for configuring the build process, such as specifying the output file name, setting the target architecture, and specifying build flags.

Go Run vs Go Build – Choosing the Right Command for Your Project

Now that you understand the differences between Go run and Go build, you can choose the right command for your project based on your specific needs.

Here are some scenarios where you might choose one command over the other:

  1. Testing small programs or code snippets

If you want to quickly test a small program or code snippet, you can use the go run command. This will compile and run the program in a single step, without the need to create an executable file.

  1. Building an executable file for deployment

You can use the go build command to build an executable file that you can deploy or distribute across different platforms. This command will create an executable file that anyone can run on any platform without needing to install Go.

  1. Debugging and profiling

If you want to debug or profile your application, you can utilize the go build command to create an executable file. This file can then be used for both debugging and profiling purposes.

  1. Configuration

If you need to configure the build process, you can use the go build command. This command provides several options for configuring the build process, such as specifying the output file name, setting the target architecture, and specifying build flags.

Wrapping Up

In conclusion, Go Run and Build Tool are powerful tools that make it easy to build, test, and run Go programs. These tools provide developers with a streamlined and efficient way to develop their Go applications, and the various flags and parameters offer a range of options and flexibility for customizing the build process.

Whether you’re building a small script or a large-scale application, Go Run and Build Tool make it easy to compile and run your code. By using these tools, you can focus on writing great code without having to worry about the build process. And with the added benefit of cross-compilation, you can even build for other platforms and architectures.

Reference

Was This Article Helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *