Chapter 2. The Go compiler
The Go compiler is a build tool and dependency manager for the Go programming language. It offers error checking and optimization of your code.
2.1. Prerequisites Copy linkLink copied to clipboard!
-
Go Toolset is installed.
For more information, see Installing Go Toolset.
2.2. Setting up a Go workspace Copy linkLink copied to clipboard!
To compile a Go program, you need to set up a Go workspace.
Procedure
Create a workspace directory as a subdirectory of
$GOPATH/src
.A common choice is
$HOME/go
.- Place your source files into your workspace directory.
Set the location of your workspace directory as an environment variable to the
$HOME/.bashrc
file by running:echo 'export GOPATH=<workspace_dir>' >> $HOME/.bashrc source $HOME/.bashrc
$ echo 'export GOPATH=<workspace_dir>' >> $HOME/.bashrc $ source $HOME/.bashrc
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Replace <workspace_dir> with the name of your workspace directory.
2.3. Compiling a Go program Copy linkLink copied to clipboard!
You can compile your Go program using the Go compiler. The Go compiler creates an executable binary file as a result of compiling.
Prerequisites
-
A set up Go workspace.
For information on how to set up a workspace, see Setting up a Go workspace.
Procedure
In your project directory, run:
On Red Hat Enterprise Linux 7:
scl enable go-toolset-1.17 'go build <output_file>'
$ scl enable go-toolset-1.17 'go build <output_file>'
Copy to Clipboard Copied! Toggle word wrap Toggle overflow -
Replace
<output_file<
with the desired name of your output file and<go_main_package>
with the name of your main package.
-
Replace
On Red Hat Enterprise Linux 8:
go build <output_file>
$ go build <output_file>
Copy to Clipboard Copied! Toggle word wrap Toggle overflow -
Replace
<output_file>
with the desired name of your output file and<go_main_package>
with the name of your main package.
-
Replace
On Red Hat Enterprise Linux 9:
go build <output_file>
$ go build <output_file>
Copy to Clipboard Copied! Toggle word wrap Toggle overflow -
Replace
<output_file>
with the desired name of your output file and<go_main_package>
with the name of your main package.
-
Replace
2.4. Running a Go program Copy linkLink copied to clipboard!
The Go compiler creates an executable binary file as a result of compiling. Complete the following steps to execute this file and run your program.
Prerequisites
-
Your program is compiled.
For more information on how to compile your program, see Compiling a Go program.
Procedure
To run your program, run in the directory containing the executable file:
./<file_name>
$ ./<file_name>
-
Replace
<file_name>
with the name of your executable file.
2.5. Installing compiled Go projects Copy linkLink copied to clipboard!
You can install already compiled Go projects to use their executable files and libraries in further Go projects. After installation, the executable files and libraries of the project are copied to according directories in the Go workspace. Its dependencies are installed as well.
Prerequisites
-
A Go workspace.
For more information, see Setting up a Go workspace.
Procedure
To install a Go project, run:
On Red Hat Enterprise Linux 7:
scl enable go-toolset-1.17 'go install <go_project>'
$ scl enable go-toolset-1.17 'go install <go_project>'
Copy to Clipboard Copied! Toggle word wrap Toggle overflow -
Replace
<go_project>
with the name of the Go project you want to install.
-
Replace
On Red Hat Enterprise Linux 8:
go install <go_project>
$ go install <go_project>
Copy to Clipboard Copied! Toggle word wrap Toggle overflow -
Replace
<go_project>
with the name of the Go project you want to install.
-
Replace
On Red Hat Enterprise Linux 9:
go install <go_project>
$ go install <go_project>
Copy to Clipboard Copied! Toggle word wrap Toggle overflow -
Replace
<go_project>
with the name of the Go project you want to install.
-
Replace
2.6. Downloading and installing Go projects Copy linkLink copied to clipboard!
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, the executable files and libraries of the project are copied to according directories in the Go workspace. Its dependencies are installed as well.
Prerequisites
-
A Go workspace.
For more information, see Setting up a Go workspace.
Procedure
To download and install a Go project, run:
On Red Hat Enterprise Linux 7:
scl enable go-toolset-1.17 'go get <third_party_go_project>'
$ scl enable go-toolset-1.17 'go get <third_party_go_project>'
Copy to Clipboard Copied! Toggle word wrap Toggle overflow -
Replace
<third_party_go_project>
with the name of the project you want to download.
-
Replace
On Red Hat Enterprise Linux 8:
go get <third_party_go_project>
$ go get <third_party_go_project>
Copy to Clipboard Copied! Toggle word wrap Toggle overflow -
Replace
<third_party_go_project>
with the name of the project you want to download.
-
Replace
On Red Hat Enterprise Linux 9:
go get <third_party_go_project>
$ go get <third_party_go_project>
Copy to Clipboard Copied! Toggle word wrap Toggle overflow -
Replace
<third_party_go_project>
with the name of the project you want to download.
-
Replace
For information on possible values of third-party projects, run:
On Red Hat Enterprise Linux 7:
scl enable go-toolset-1.17 'go help importpath'
$ scl enable go-toolset-1.17 'go help importpath'
Copy to Clipboard Copied! Toggle word wrap Toggle overflow On Red Hat Enterprise Linux 8:
go help importpath
$ go help importpath
Copy to Clipboard Copied! Toggle word wrap Toggle overflow On Red Hat Enterprise Linux 9:
go help importpath
$ go help importpath
Copy to Clipboard Copied! Toggle word wrap Toggle overflow