Search

Using Go 1.20.10 Toolset

download PDF
Red Hat Developer Tools 1

Installing and using Go 1.20.10 Toolset

Jacob Valdez

Red Hat Developer Group Documentation Team

Abstract

Go Toolset is a Red Hat offering for developers on the Red Hat Enterprise Linux (RHEL) operating system. Use this guide for an overview of Go Toolset, to learn how to invoke and use different versions of Go tools, and to find resources with more in-depth information.

Making open source more inclusive

Red Hat is committed to replacing problematic language in our code, documentation, and web properties. We are beginning with these four terms: master, slave, blacklist, and whitelist. Because of the enormity of this endeavor, these changes will be implemented gradually over several upcoming releases. For more details, see our CTO Chris Wright’s message.

Providing feedback on Red Hat documentation

We appreciate your feedback on our documentation. Let us know how we can improve it.

Submitting comments on specific passages

  1. View the documentation in the Multi-page HTML format and ensure that you see the Feedback button in the upper right corner after the page fully loads.
  2. Use your cursor to highlight the part of the text that you want to comment on.
  3. Click the Add Feedback button that appears near the highlighted text.
  4. Add your feedback and click Submit.

Submitting feedback through Jira (account required)

  1. Log in to the Jira website.
  2. Click Create in the top navigation bar
  3. Enter a descriptive title in the Summary field.
  4. Enter your suggestion for improvement in the Description field. Include links to the relevant parts of the documentation.
  5. Click Create at the bottom of the dialogue.

Chapter 1. Go Toolset

Go Toolset is a Red Hat offering for developers on Red Hat Enterprise Linux (RHEL). It provides the Go programming language tools and libraries. Note that Go is alternatively known as golang.

Go Toolset is available as a module for Red Hat Enterprise Linux 8. Go Toolset is available as packages for Red Hat Enterprise Linux 9.

1.1. Go Toolset components

The following components are available as a part of Go Toolset:

NameVersionDescription

golang

RHEL 8 — 1.20.10, RHEL 9 — 1.20.10

A Go compiler.

delve

RHEL 8 — 1.20.2, RHEL 9 — 1.20.2

A Go debugger.

1.2. Go Toolset compatibility

Go Toolset is available for Red Hat Enterprise Linux 8 and Red Hat Enterprise Linux 9 on the following architectures:

  • AMD and Intel 64-bit
  • 64-bit ARM
  • IBM Power Systems, Little Endian
  • 64-bit IBM Z

1.3. Installing Go Toolset

Complete the following steps to install Go Toolset, including all dependent packages.

Prerequisites

  • All available Red Hat Enterprise Linux updates are installed.

Procedure

On Red Hat Enterprise Linux 8, install the go-toolset module by running:

# yum module install go-toolset

On Red Hat Enterprise Linux 9, install the go-toolset package by running:

# dnf install go-toolset

1.4. Installing Go documentation

You can install documentation for the Go programming language on your local system.

Prerequisites

Procedure

To install the golang-docs package, run the following command:

  • On Red Hat Enterprise Linux 8:

    # yum install golang-docs

    You can find the documentation under the following path: /usr/lib/golang/doc/go_spec.html.

  • On Red Hat Enterprise Linux 9:

    # dnf install golang-docs

    You can find the documentation under the following path: /usr/lib/golang/doc/go_spec.html.

1.5. Additional resources

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.

Chapter 3. The gofmt formatting tool

Instead of a style guide, the Go programming language uses the gofmt code formatting tool. gofmt automatically formats your code according to the Go layout rules.

3.1. Prerequisites

3.2. Formatting code

You can use the gofmt formatting tool to format code in a given path. When the path leads to a single file, the changes apply only to the file. When the path leads to a directory, all .go files in the directory are processed.

Procedure

To format your code in a given path, run:

  • On Red Hat Enterprise Linux 8:

    $ gofmt -w <code_path>
    • Replace <code_path> with the path to the code you want to format.
  • On Red Hat Enterprise Linux 9:

    $ gofmt -w <code_path>
    • Replace <code_path> with the path to the code you want to format.
Note

To print the formatted code to standard output instead of writing it to the original file, omit the -w option.

3.3. Previewing changes to code

You can use the gofmt formatting tool to preview changes done by formatting code in a given path. The output in unified diff format is printed to standard output.

Procedure

To show differences in your code in a given path, run:

  • On Red Hat Enterprise Linux 8:

    $ gofmt -w -d <code_path>
    • Replace <code_path> with the path to the code you want to compare.
  • On Red Hat Enterprise Linux 9:

    $ gofmt -w -d <code_path>
    • Replace <code_path> with the path to the code you want to compare.

3.4. Simplifying code

You can use the gofmt formatting tool to simplify your code.

Procedure

  • To simplify anf format code in a given path, run:

    • On Red Hat Enterprise Linux 8:

      $ gofmt -s -w <code_path>
      • Replace <code_path> with the path to the code you want to simplify.
    • On Red Hat Enterprise Linux 9:

      $ gofmt -s -w <code_path>
      • Replace <code_path> with the path to the code you want to simplify.

3.5. Refactoring code

You can use the gofmt formatting tool to refactor your code by applying arbitrary substitutions.

Procedure

  • To refactor and format your code in a given path, run:

    • On Red Hat Enterprise Linux 8:

      $ gofmt -r -w <rewrite_rule> <code_path>
      • Replace <code_path> with the path to the code you want to refactor and <rewrite_rule> with the rule you want it to be rewritten by.
    • On Red Hat Enterprise Linux 9:

      $ gofmt -r -w <rewrite_rule> <code_path>
      • Replace <code_path> with the path to the code you want to refactor and <rewrite_rule> with the rule you want it to be rewritten by.

3.6. Additional resources

Chapter 4. The Go race detector

Go Toolset includes the Go race detector, which is a tool of the Go standard library for finding race conditions.
Note that the race detector has a significant runtime resource overhead.

4.1. Prerequisites

4.2. Using the Go race detector

Use the Go race detector to check your code for race conditions.

Procedure

To use the race detector, run:

  • On Red Hat Enterprise Linux 8:

    $ go build -race -o <output_file> <go_main_package>
    • Replace <output_file> with the name of your executable file and <go_main_package> with the name of the package you want to test.
  • On Red Hat Enterprise Linux 9:

    $ go build -race -o <output_file> <go_main_package>
    • Replace <output_file> with the name of your executable file and <go_main_package> with the name of the package you want to test.

4.3. Additional resources

Chapter 5. Container images with Go Toolset

You can build your own Go Toolset containers from either Red Hat Enterprise Linux container images or Red Hat Universal Base Images (UBI).

5.1. Red Hat Enterprise Linux Go Toolset container images contents

The Red Hat Enterprise Linux 8 and Red Hat Enterprise Linux 9 container images of Go Toolset contain the following packages:

ComponentVersionPackage

Go

1.20

RHEL 8 — go-toolset-1.20.10 RHEL 9 — go-toolset-1.20.10

5.2. Accessing Red Hat Enterprise Linux container images

Pull the container image from the Red Hat registry before running your container and performing actions.

Procedure

To pull the required image, run:

  • For an image based on Red Hat Enterprise Linux 8:

    # podman pull registry.redhat.io/rhel8/go-toolset
  • For an image based on Red Hat Enterprise Linux 9:

    # podman pull registry.redhat.io/rhel9/go-toolset

5.3. Accessing the UBI Go Toolset container image on RHEL 8

On RHEL 8, install the UBI Go Toolset container image to access Go Toolset.
Alternatively, you can install Go Toolset to the RHEL 8 base UBI container image. For further information, see Accessing Go Toolset from the base UBI container image on RHEL 8.

Procedure

To pull the UBI Go Toolset container image from the Red Hat registry, run:

# podman pull registry.access.redhat.com/ubi8/go-toolset

5.4. Accessing Go Toolset from the base UBI container image on RHEL 8

On RHEL 8, Go Toolset packages are part of the Red Hat Universal Base Images (UBIs) repositories, which means you can install Go Toolset as an addition to the base UBI container image. To keep the container image size small, install only individual packages instead of the entire Go Toolset.
Alternatively, you can install the UBI Go Toolset container image to access Go Toolset. For further information, see Accessing the UBI Go Toolset container image on RHEL 8.

Prerequisites

  • An existing Containerfile.
    For information on creating Containerfiles, see the Dockerfile reference page.

Procedure

  • To create a container image containing Go Toolset, add the following lines to your Containerfile:
FROM registry.access.redhat.com/ubi8/ubi:latest

RUN yum module install -y go-toolset
  • To create a container image containing an individual package only, add the following lines to your Containerfile:

    RUN yum install -y <package-name>
    • Replace <package-name> with the name of the package you want to install.

5.5. Additional resources

Chapter 6. Changes in Go Toolset

Go Toolset has been updated from version 1.19 to 1.20.10 on RHEL 8 and RHEL 9.

Notable changes include:

  • New functions added in the unsafe package to handle slices and strings without depending on the internal representation.
  • Comparable types can now satisfy comparable constraints.
  • A new crypto/ecdh package.
  • The go build and go test commands no longer accept the -i flag.
  • The go generate and go test commands now accept the -skip pattern option.
  • The go build, go install, and other build-related commands now support the -pgo and -cover flags.
  • The go command now disables cgo by default on systems without a C toolchain.
  • The go version -m command now supports reading more Go binaries types.
  • The go command now disables cgo by default on systems without a C toolchain.
  • Added support for collecting code coverage profiles from applications and integration tests instead of collecting them only from unit tests.

For detailed information regarding the updates, see the upstream Go 1.20 Release Notes.

Legal Notice

Copyright © 2023 Red Hat, Inc.
The text of and illustrations in this document are licensed by Red Hat under a Creative Commons Attribution–Share Alike 3.0 Unported license ("CC-BY-SA"). An explanation of CC-BY-SA is available at http://creativecommons.org/licenses/by-sa/3.0/. In accordance with CC-BY-SA, if you distribute this document or an adaptation of it, you must provide the URL for the original version.
Red Hat, as the licensor of this document, waives the right to enforce, and agrees not to assert, Section 4d of CC-BY-SA to the fullest extent permitted by applicable law.
Red Hat, Red Hat Enterprise Linux, the Shadowman logo, the Red Hat logo, JBoss, OpenShift, Fedora, the Infinity logo, and RHCE are trademarks of Red Hat, Inc., registered in the United States and other countries.
Linux® is the registered trademark of Linus Torvalds in the United States and other countries.
Java® is a registered trademark of Oracle and/or its affiliates.
XFS® is a trademark of Silicon Graphics International Corp. or its subsidiaries in the United States and/or other countries.
MySQL® is a registered trademark of MySQL AB in the United States, the European Union and other countries.
Node.js® is an official trademark of Joyent. Red Hat is not formally related to or endorsed by the official Joyent Node.js open source or commercial project.
The OpenStack® Word Mark and OpenStack logo are either registered trademarks/service marks or trademarks/service marks of the OpenStack Foundation, in the United States and other countries and are used with the OpenStack Foundation's permission. We are not affiliated with, endorsed or sponsored by the OpenStack Foundation, or the OpenStack community.
All other trademarks are the property of their respective owners.
Red Hat logoGithubRedditYoutubeTwitter

Learn

Try, buy, & sell

Communities

About Red Hat Documentation

We help Red Hat users innovate and achieve their goals with our products and services with content they can trust.

Making open source more inclusive

Red Hat is committed to replacing problematic language in our code, documentation, and web properties. For more details, see the Red Hat Blog.

About Red Hat

We deliver hardened solutions that make it easier for enterprises to work across platforms and environments, from the core datacenter to the network edge.

© 2024 Red Hat, Inc.