Compile Go programs
Compile Go source code into executables and libraries on Red Hat Enterprise Linux with the Go compiler in Go Toolset by using a Red Hat-supported toolchain for building command-line tools, services, and shared libraries.
Prerequisites Copy linkLink copied!
- Go Toolset is installed.
Configure a Go workspace Copy linkLink copied!
Modern Go projects are built using modules. You can start with a single module and then optionally group multiple modules into a workspace to work on them simultaneously.
Procedure Copy linkLink copied!
Compile a Go program Copy linkLink copied!
You can compile Go sources in the current directory by running go build to produce an executable binary.
Before you begin Copy linkLink copied!
- A Go workspace with configured modules.
Procedure Copy linkLink copied!
$ go build .
Run a Go program Copy linkLink copied!
The Go compiler creates an executable binary file as a result of compiling. Complete the following steps to run your program.
Procedure Copy linkLink copied!
- To run a compiled Go program, enter:
$ ./<file_name>Replace
<file_name>with the name of your executable file. - To compile the Go sources in the current directory and run the program in a single step, enter:
$ go run .
Install compiled Go projects Copy linkLink copied!
You can download and install third-party Go projects from online resources to use their executable files and libraries in further Go projects. After installation, Go copies the project’s executables and libraries to workspace directories and installs its dependencies as well.
Before you begin Copy linkLink copied!
- A Go workspace with configured modules.
Procedure Copy linkLink copied!
$ go install <go_project>
Download and install Go projects Copy linkLink copied!
Use go install with a third-party import path to download and install Go commands and libraries into your workspace. Optionally, run go help importpath for details on how to specify remote projects.
Before you begin Copy linkLink copied!
- A Go workspace with configured modules.