Introduction and Prerequisites

In this tutorial, you will install the Go 1.11.5 Compiler and build a simple Go Hello World application. This tutorial should take less than 30 minutes to complete.

Before you begin, you will need a current Red Hat Enterprise Linux 7 workstation or server subscription that allows you to download software and get updates from Red Hat. If you don’t have an active subscription, register and obtain the RHEL Developer subscription (includes RHEL server) from here.

NOTE: The Go Compiler naming convention has changed so that the version number reflects the community version. The previous version was Go-toolset-7 which was based on Go 1.10.

If you encounter difficulties at any point, see Troubleshooting and FAQ.

1. Enable necessary software repositories

In this step, you will configure and “register” your system to obtain software from the devtools repository by using the command line interface. 

Note: registering a subscription (the steps to follow) is different than registering for the Red Hat Developer Program (which you’ve likely done by now).

You can add or remove software repositories from the command line using the subscription-manager tool as the root user. Use the --list option to view the available software repositories and verify that you have access to the devtools repo.

$ su -
# subscription-manager repos --list | egrep devtools

If you don’t see any devtools repositories in the list, your subscription might not include it. See Troubleshooting and FAQ for more information.

If you are using a desktop edition of Red Hat Enterprise Linux, change -server- to -desktop- in the following commands:

# subscription-manager repos --enable rhel-7-server-devtools-rpms
# subscription-manager repos --enable rhel-server-rhscl-7-rpms

Add the Red Hat Developer Tools key to your system:

# cd /etc/pki/rpm-gpg
# wget -O RPM-GPG-KEY-redhat-devel https://www.redhat.com/security/data/a5787476.txt
# rpm --import RPM-GPG-KEY-redhat-devel

2. Setup your development environment

In this next step you will use a single command to download and install the Go Compiler toolchain from the Red Hat repository. The length of time this step takes depends on the speed of your Internet connection and your system. With a reasonably fast connection, this step should complete in well under 5 minutes.

  1. To install all components that are included in Go Toolset, install the go-toolset-1.11 package:

# yum install go-toolset-1.11

This installs all development and debugging tools, and other dependent packages to the system.

  1. Create the Go language workspace directory and environment variable:
$ mkdir -p workspace_dir 
$ echo 'export GOPATH=workspace_dir' >> $HOME/.bashrc 
$ source $HOME/.bashrc 

Select an appropriate value for the workspace_dir directory. A common choice is $HOME/go.

If the GOPATH variable is not set, the go compiler uses the ~/go directory.

3. Hello World and your first application

To compile a Go program on the command line, run the go compiler as follows:
 

# scl enable go-toolset-1.11 'go build -o output_file go_main_package’

This creates a binary file named output_file in the current working directory. If the -o option is omitted, the compiler creates a file named after the go_main_package, "go_main_package". If go_main_package is not main package or if multiple projects or *.go files are specified resulting binaries are discarded. In this use go build work as verification that the supplied projects/files are buildable.

Note that you can execute any command using the scl utility, causing it to be run with the Go Toolset binaries. This allows you to run a shell session with Go Toolset as default:

scl enable go-toolset-1.11 'bash'

Using the Go Compiler

Example: Compiling a Go Program on the Command Line

Consider a source file named hello.go with the following contents:

       package main

       import (
           "fmt"
           "net/http"
       )

       func Welcome(w http.ResponseWriter, req *http.Request) {

           fmt.Fprintf(w, "<h1>Welcome to the Go toolset.</h1>")

       }

       func main() {

           fmt.Println("Hello.")
           fmt.Println("Starting http server.")
           // Register handler function
           http.HandleFunc("/welcome", Welcome)
           fmt.Println("Go to localhost:8080/welcome To terminate press CTRL+C")
           // Start server
            http.ListenAndServe(":8080", nil)

       }

   To compile this source code on the command line by using the Go compiler from Red Hat Go Developer Toolset, type:
$ scl enable go-toolset-1.11 'go build hello.go'

This creates a new binary file called hello in the current working directory.

Running a C Program

When Go compiler compiles a program, it creates an executable binary file. To run this program on the command line, change to the directory with the executable file and type:

 

  ./file_name

 

Example: Running a C Program on the Command Line

Assuming that you have successfully compiled the hello binary file as shown in previous Example, “Compiling a Go Program on the Command Line”, you can run it by typing the following at a shell prompt:

 

   ./hello
   Hello.
   Starting http server.
   Go to localhost:8080/welcome To terminate press CTRL+C

 

Working with Software Collection packages

These tools are packaged as software collections which  are designed to allow multiple versions of software to be installed concurrently. To accomplish this, the desired package is added to your runtime environment as needed with the scl enable command. When scl enable runs, it modifies environment variables and then runs the specified command. The environmental changes only affect the command that is run by scl and any processes that are run from that command. The steps in this tutorial run the command bash to start a new interactive shell to work in the updated environment. The changes aren’t permanent. Typing exit will return to the original shell with the original environment. Each time you login, or start a new terminal session, scl enable needs to be run again.

While it is possible to change the system profile to make RHSCL packages part of the system’s global environment, this is not recommended. Doing this can cause conflicts and unexpected problems with other applications because the system version of the package is obscured by having the RHSCL version in the path first.

Learn more about Red Hat Software Collections

Red Hat Software Collections deliver the latest stable versions of dynamic languages, open source databases, and web development tools that can be deployed alongside those included in Red Hat Enterprise Linux. Red Hat Software Collections is available with select Red Hat Enterprise Linux subscriptions and has a three-year life cycle to allow rapid innovation without sacrificing stability. For more information:

You can view the list of packages available in RHSCL by running:

$ yum --disablerepo="*" --enablerepo="rhel-server-rhscl-7-rpms" list available

Developing with Red Hat Enterprise Linux

Red Hat Enterprise Linux 7 Developer Guide — The developer guide for Red Hat Enterprise Linux 7 provides an introduction to application development tools and using source code management tools such as Git in Red Hat Enterprise Linux 7.

Become a Red Hat developer and join the Red Hat Developer Program

Red Hat delivers the resources and ecosystem of experts to help you be more productive and build great solutions. Register for free at developers.redhat.com.

 

Troubleshooting and FAQ

  1. As a developer, how can I get a Red Hat Enterprise Linux subscription that includes the Clang/LLVM, Go, or Rust Compilers?

Developers can get a no-cost Red Hat Enterprise Linux Developer subscription for development purposes by registering and downloading through developers.redhat.com. We recommend you follow our Getting Started Guide which covers downloading and installing Red Hat Enterprise Linux on a physical system or virtual machine (VM) using your choice of VirtualBox, VMware, Microsoft Hyper-V, or Linux KVM/Libvirt. For more information, see Frequently asked questions: no-cost Red Hat Enterprise Linux Developer subscription.

  1. I can’t find the devtools or RHSCL repository on my system.

Some Red Hat Enterprise Linux subscriptions do not include access to Software Collections or development tools. For a list of what subscriptions are included see How to use Red Hat Software Collections (RHSCL), Red Hat Developer Toolset (DTS, etc.).

The name of the repository depends on whether you have a server or workstation version of Red Hat Enterprise Linux installed. You can use subscription-manager to view the available software repositories and verify that you have access to RHSCL and devtools:

$ su -
# subscription-manager repos --list | egrep rhscl
# subscription-manager repos --list | egrep devtools

 

  1. When I run yum install package (go-toolset-7, llvm-toolset-7, rust-toolset-7), it fails due to a missing dependency.

Some software collections require packages that are in the devtools RPMs repository, which is not enabled by default. See Step 1 above, for how to enable both the applicable RPMs and repositories.

  1. How can I view the Go manual pages?

The Go compiler help command provides information on its usage. To show the help index:

$ scl enable go-toolset-7 'go help'
  1. How can I find out what RHSCL packages are installed?

scl --list will show the list of RHSCL packages that have been installed, whether they are enabled or not.

$ scl --list
Last updated: August 29, 2023