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

2.2. Setting up a Go workspace

To compile a Go program, you need to set up a Go workspace.

Procedure

  1. Create a workspace directory as a subdirectory of $GOPATH/src.

    A common choice is $HOME/go.

  2. Place your source files into your workspace directory.
  3. 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

    Replace <workspace_dir> with the name of your workspace directory.

Additional resources

2.3. Compiling a Go program

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 with configured modules.
    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 8:

    $ go build -o <output_file> <go_main_package>
    • Replace <output_file> with the desired name of your output file and <go_main_package> with the name of your main package.
  • On Red Hat Enterprise Linux 9:

    $ go build -o <output_file> <go_main_package>
    • Replace <output_file> with the desired name of your output file and <go_main_package> with the name of your main package.

2.4. Running a Go program

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

Procedure

To run your program, run in the directory containing the executable file:

$ ./<file_name>
  • Replace <file_name> with the name of your executable file.

2.5. Installing compiled Go projects

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

Procedure

To install a Go project, run:

  • On Red Hat Enterprise Linux 8:

    $ go install <go_project>
    • Replace <go_project> with the name of the Go project you want to install.
  • On Red Hat Enterprise Linux 9:

    $ go install <go_project>
    • Replace <go_project> with the name of the Go project you want to install.

2.6. Downloading and installing Go projects

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

Procedure

  • To download and install a Go project, run:

    • On Red Hat Enterprise Linux 8:

      $ go install <third_party_go_project>
      • Replace <third_party_go_project> with the name of the project you want to download.
    • On Red Hat Enterprise Linux 9:

      $ go install <third_party_go_project>
      • Replace <third_party_go_project> with the name of the project you want to download.
  • For information on possible values of third-party projects, run:

    • On Red Hat Enterprise Linux 8:

      $ go help importpath
    • On Red Hat Enterprise Linux 9:

      $ go help importpath

2.7. Additional resources

  • For more information on the Go compiler, see the official Go documentation.
  • To display the help index included in Go Toolset, run:

    • On Red Hat Enterprise Linux 8:

      $ go help
    • On Red Hat Enterprise Linux 9:

      $ go help
  • To display documentation for specific Go packages, run:

    • On Red Hat Enterprise Linux 8:

      $ go doc <package_name>
    • On Red Hat Enterprise Linux 9:

      $ go doc <package_name>

      See Go packages for an overview of Go packages.