Using Go 1.26.2 Toolset

Red Hat Developer Tools 1

Installing and using Go 1.26.2 Toolset

Red Hat Customer Content Services

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.

Providing feedback on Red Hat documentation

We are committed to providing high-quality documentation and value your feedback. To help us improve, you can submit suggestions or report errors through the Red Hat Jira tracking system.

Procedure

  1. Log in to the Content from redhat.atlassian.net is not included.Jira website.

    If you do not have an account, select the option to create one.

  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 window.

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 RHEL 8 and as packages for RHEL 9 and 10.

1.1. Go Toolset components

The following table lists the main Go Toolset packages, their versions, and a short description of each.

Table 1.1. Packages included in Go Toolset

NameVersionDescription

golang

1.26.2

A Go compiler.

delve

1.26.2

A Go debugger.

1.2. Go Toolset compatibility

Go Toolset is available for Red Hat Enterprise Linux on AMD and Intel 64-bit (x86_64), 64-bit ARM (aarch64), IBM Power Systems, Little Endian (ppc64le), and 64-bit IBM Z (s390x) architectures.

1.3. Installing Go Toolset

Install Go Toolset and all dependent packages on Red Hat Enterprise Linux by enabling the go-toolset module on RHEL 8 or installing the go-toolset package on RHEL 9 and 10.

Prerequisites

  • All available Red Hat Enterprise Linux updates are installed.

Procedure

  • Install Go Toolset:

    • On RHEL 8, enter:

      # yum module install go-toolset
    • On RHEL 9 and 10, enter:

      # dnf install go-toolset

1.4. Installing Go documentation

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

Procedure

  • Install the golang-docs package:

    • On RHEL 8, enter:

      # yum install golang-docs
    • On RHEL 9 and 10, enter:

      # dnf install golang-docs

Verification

  • Open /usr/lib/golang/doc/go_spec.html in a browser that is installed on the same host.

1.5. Additional resources

Chapter 2. The Go compiler

The Go compiler in Go Toolset builds Go source code into executables and libraries.

2.1. Prerequisites

  • Go Toolset is installed.

2.2. Setting up a Go workspace

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

  1. Create a root directory for your projects, for example:

    $ mkdir ~/go-projects/
  2. Change into the project directory:

    $ cd ~/go-projects/
  3. Initialize a module:

    1. Create a directory for your module:

      $ mkdir <module_name>
    2. Change into the module’s directory:

      $ cd <module_name>
    3. Initialize the module:

      $ go mod init <module_name>

      This command creates a single-module project.

    If you want to create multiple modules, repeat this step for every module.

  4. If you want to work on multiple modules at the same time, create a multi-module workspace:

    1. Change into the project directory:

      $ cd ~/go-projects/
    2. Initialize a workspace to include multiple modules:

      $ go work init <module_name_1> <module_name_n> ...

2.3. Compiling a Go program

You can compile Go sources in the current directory by running go build to produce an executable binary.

Prerequisites

  • A Go workspace with configured modules.

Procedure

  • Compile the Go sources in the current directory:

    $ go build .

Additional resources

2.4. Running a Go program

The Go compiler creates an executable binary file as a result of compiling. Complete the following steps to run your program.

Procedure

  • Use one of the following options to run your Go program:

    • 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 .

2.5. Installing compiled 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 according to the directories in the Go workspace. Its dependencies are installed as well.

Prerequisites

  • A Go workspace with configured modules.

Procedure

  • Install a Go project:

    $ go install <go_project>

Additional resources

2.6. Downloading and installing Go projects

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.

Prerequisites

  • A Go workspace with configured modules.

Procedure

  1. To download and install a Go project, enter:

    $ go install <third_party_go_project>
  2. Optional: For information on possible values of third-party projects, enter:

    $ go help importpath

Additional resources

2.7. Additional resources

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

  • Go Toolset is installed.

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, enter:

    $ 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

  • Show differences in your code in a given path:

    $ gofmt -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

  1. To simplify code in a given path, enter:

    $ gofmt -s -w <code_path>

    Replace <code_path> with the path to the code you want to simplify.

  2. To apply the changes, enter:

    $ gofmt -w <code_path>

    Replace <code_path> with the path to the code you want to format.

3.5. Refactoring code

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

Procedure

  1. To refactor your code in a given path, enter:

    # 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.

  2. To apply the changes, enter:

    # gofmt -w <code_path>

    Replace <code_path> with the path to the code you want to format.

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

  • Use the race detector:

    # 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 following table lists the Go component shipped in Red Hat Enterprise Linux Go Toolset container images, its version, and the package name.

Table 5.1. Go Toolset component in container images

ComponentVersionPackage

Go

1.26.2

go-toolset-1.26.2

5.2. Pulling the RHEL-based Go Toolset container image

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

Procedure

  • Pull the required image:

    • For an image based on RHEL 8, enter:

      # podman pull registry.redhat.io/rhel8/go-toolset
    • For an image based on RHEL 9, enter:

      # podman pull registry.redhat.io/rhel9/go-toolset
    • For an image based on RHEL 10, enter:

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

5.3. Pulling the UBI-based Go Toolset container image

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

Procedure

  • Pull the required image:

    • For an image based on RHEL 8, enter:

      # podman pull registry.access.redhat.com/ubi8/go-toolset
    • For an image based on RHEL 9, enter:

      # podman pull registry.access.redhat.com/ubi9/go-toolset
    • For an image based on RHEL 10, enter:

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

5.4. Creating a custom UBI-based container with Go Toolset

Go Toolset packages are in Red Hat Universal Base Images (UBI) repositories. You can add them to a base UBI container image. To keep the image small, install 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 Pulling the UBI-based Go Toolset container image.

Prerequisites

Procedure

  • To create a container image containing Go Toolset, add the following to your container file:

    • For an image based on RHEL 8, enter:

      FROM registry.access.redhat.com/ubi8/ubi:latest
      
      RUN yum module install -y go-toolset
    • For an image based on RHEL 9, enter:

      FROM registry.access.redhat.com/ubi9/ubi:latest
      
      RUN yum install -y go-toolset
    • For an image based on RHEL 10, enter:

      FROM registry.access.redhat.com/ubi10/ubi:latest
      
      RUN yum install -y go-toolset

5.5. Additional resources

Chapter 6. Changes in Go Toolset 1.26.2

Go Toolset has been updated to version 1.26.2.

  • Maintenance release:

    • Go 1.26.2 includes security fixes to the go command, the compiler, and the archive/tar, crypto/tls, crypto/x509, html/template, and os packages. It also includes bug fixes to the go command, the go fix command, the compiler, the linker, the runtime, and the net, net/http, and net/url packages.
  • Language:

    • The built-in new function accepts an expression operand that sets the initial value of the allocated variable.
    • A generic type can refer to itself in its type parameter list, which allows recursive constraints such as a type that must be instantiated with a type like itself.
  • Runtime and performance:

    • The Green Tea garbage collector, which improves marking and scanning of small objects, is enabled by default. If you must opt out at build time, set GOEXPERIMENT=nogreenteagc. This setting is scheduled for removal in Go 1.27.
    • Baseline overhead for cgo calls is reduced.
    • On 64-bit platforms, the runtime randomizes the heap base address at process start to harden cgo binaries against exploitation. If you must opt out at build time, set GOEXPERIMENT=norandomizedheapbase64.
  • Tools:

    • The go fix command is reimplemented around modernizers that update projects to current idioms and library APIs. Historical go fix fixers were removed.
    • The cmd/doc helper and the go tool doc command are removed. Use go doc with the same flags and arguments.
    • After you run go mod init, new go.mod files default the go directive to the previous supported minor release series (for example, the Go 1.26.2 toolchain defaults new modules to go 1.25). Adjust the directive with go get go@version if you need a different minimum version.
    • The pprof tool web UI, which you enable with the -http flag, defaults to the flame graph view.
  • Compiler:

    • The compiler can allocate backing storage for slices on the stack in more situations, which can improve performance.
  • Standard library (highlights):

    • The new crypto/hpke package implements Hybrid Public Key Encryption (HPKE) per RFC 9180, including post-quantum hybrid KEMs.
    • Post-quantum hybrid TLS key exchanges are enabled by default in crypto/tls. You can adjust curve preferences or use the tlssecpmlkem=0 GODEBUG setting where required.
    • The net/url package rejects some malformed URLs that contain misplaced colons in the host portion. The urlstrictcolons=0 GODEBUG setting restores the previous parsing behavior.

For maintenance details in Go 1.26.2, see the Content from go.dev is not included.Go release history and the Content from github.com is not included.Go 1.26.2 milestone. For Go 1.26 feature changes, see the Content from go.dev is not included.upstream Go 1.26 release notes.

Go Toolset is a rolling Application Stream, and Red Hat supports only the latest version. For more information, see the Red Hat Enterprise Linux Application Streams Life Cycle document.

Legal Notice

Copyright © Red Hat.
Except as otherwise noted below, the text of and illustrations in this documentation are licensed by Red Hat under the Creative Commons Attribution–Share Alike 3.0 Unported license . 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, the Red Hat logo, JBoss, Hibernate, and RHCE are trademarks or registered trademarks of Red Hat, LLC. or its subsidiaries in the United States and other countries.
Linux® is the registered trademark of Linus Torvalds in the United States and other countries.
XFS is a trademark or registered trademark of Hewlett Packard Enterprise Development LP or its subsidiaries in the United States and other countries.
The OpenStack® Word Mark and OpenStack logo are trademarks or registered trademarks of the Linux Foundation, used under license.
All other trademarks are the property of their respective owners.