Using Go Toolset
Installing and using Go Toolset 1.11
Abstract
Chapter 1. Go Toolset Copy linkLink copied to clipboard!
1.1. About Go Toolset Copy linkLink copied to clipboard!
Go Toolset is a Red Hat offering for developers on the Red Hat Enterprise Linux platform. It provides the Go programming language tools and libraries. Go is alternatively known as golang.
Go Toolset is distributed as a part of Red Hat Developer Tools for Red Hat Enterprise Linux 7 and is available as a module in Red Hat Enterprise Linux 8.
The following components are available as a part of Go Toolset:
| Name | Version | Description |
|---|---|---|
| golang | 1.11.5 | A Go compiler. |
1.2. Compatibility Copy linkLink copied to clipboard!
Go Toolset is available for Red Hat Enterprise Linux 7 and Red Hat Enterprise Linux 8 on the following architectures:
- The 64-bit Intel and AMD architectures
- The 64-bit ARM architecture
- The IBM Power Systems architecture
- The little-endian variant of IBM Power Systems architecture
- The IBM Z Systems architecture
1.3. Getting Access to Go Toolset on Red Hat Enterprise Linux 7 Copy linkLink copied to clipboard!
Go Toolset is an offering that is distributed as a part of the Red Hat Developer Tools content set, which is available to customers with deployments of Red Hat Enterprise Linux 7. In order to install Go Toolset, enable the Red Hat Developer Tools and Red Hat Software Collections repositories by using the Red Hat Subscription Management and add the Red Hat Developer Tools GPG key to your system.
Enable the
rhel-7-variant-devtools-rpmsrepository:subscription-manager repos --enable rhel-7-variant-devtools-rpms
# subscription-manager repos --enable rhel-7-variant-devtools-rpmsCopy to Clipboard Copied! Toggle word wrap Toggle overflow Replace variant with the Red Hat Enterprise Linux system variant (
serverorworkstation).NoteWe recommend developers to use Red Hat Enterprise Linux Server for access to the widest range of development tools.
Enable the
rhel-variant-rhscl-7-rpmsrepository:subscription-manager repos --enable rhel-variant-rhscl-7-rpms
# subscription-manager repos --enable rhel-variant-rhscl-7-rpmsCopy to Clipboard Copied! Toggle word wrap Toggle overflow Replace variant with the Red Hat Enterprise Linux system variant (
serverorworkstation).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
# 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-develCopy to Clipboard Copied! Toggle word wrap Toggle overflow
Once the subscription is attached to the system, and repositories enabled you can install Red Hat Go Toolset as described in Section 1.4, “Installing Go Toolset”.
Additional Resources
- For more information on how to register your system using Red Hat Subscription Management and associate it with subscriptions, see the Red Hat Subscription Management collection of guides.
- For detailed instructions on subscription to Red Hat Software Collections, see the Red Hat Developer Toolset User Guide, Section 1.4. Getting Access to Red Hat Developer Toolset.
1.4. Installing Go Toolset Copy linkLink copied to clipboard!
Go Toolset is distributed as a collection of RPM packages that can be installed, updated, uninstalled, and inspected by using the standard package management tools that are included in Red Hat Enterprise Linux. Note that a valid subscription that provides access to the Red Hat Developer Tools content set is required in order to install Go Toolset on a Red Hat Enterprise Linux 7 system. For detailed instructions on how to associate your Red Hat Enterprise Linux 7 system with an appropriate subscription and get access to Go Toolset, see Section 1.3, “Getting Access to Go Toolset on Red Hat Enterprise Linux 7”.
Before installing Go Toolset, install all available Red Hat Enterprise Linux updates.
Install all of the components included in Go Toolset for your operating system:
On Red Hat Enterprise Linux 7, install the go-toolset-1.11 package:
yum install go-toolset-1.11
# yum install go-toolset-1.11Copy to Clipboard Copied! Toggle word wrap Toggle overflow On Red Hat Enterprise Linux 8, install the go-toolset module:
yum module install go-toolset
# yum module install go-toolsetCopy to Clipboard Copied! Toggle word wrap Toggle overflow This installs all development and debugging tools, and other dependent packages to the system.
Create the Go language workspace directory and environment variable:
mkdir -p workspace_dir echo 'export GOPATH=workspace_dir' >> $HOME/.bashrc source $HOME/.bashrc
$ mkdir -p workspace_dir $ echo 'export GOPATH=workspace_dir' >> $HOME/.bashrc $ source $HOME/.bashrcCopy to Clipboard Copied! Toggle word wrap Toggle overflow Select an appropriate value for the workspace_dir directory. A common choice is
$HOME/go.If the
GOPATHvariable is not set, the go compiler uses the~/godirectory.
Additional Resources
- Workspaces — Description of the Go language workspace organization. Official documentation for the Go programming language.
1.5. Additional Resources Copy linkLink copied to clipboard!
A detailed description of the Go Toolset and all its features is beyond the scope of this book. For more information, see the resources listed below.
Online Documentation
- The Go programming language Documentation — Official documentation for the Go programming language, tools, and libraries.
Chapter 2. go Copy linkLink copied to clipboard!
go is a build tool and dependency manager for the Go programming language.
Go Toolset is distributed with go 1.11.5.
2.1. Installing go Copy linkLink copied to clipboard!
In Go Toolset on Red Hat Enterprise Linux 7, go is provided by the go-toolset-1.11-golang package and is automatically installed with the go-toolset-1.11 package. On Red Hat Enterprise Linux 8, go is provided by the go-toolset module. See Section 1.4, “Installing Go Toolset”.
2.2. Writing Go 1.11.5 Programs Copy linkLink copied to clipboard!
When creating a Go program, developers must follow the rules for Go workspace layout. The .go source files must be placed in subdirectory of $GOPATH/src.
Example 2.1. Creating a Go Program
Consider a program named hello consisting of a single source file named hello.go:
mkdir -p $GOPATH/src/hello cd $GOPATH/src/hello touch hello.go
$ mkdir -p $GOPATH/src/hello
$ cd $GOPATH/src/hello
$ touch hello.go
Edit the file hello.go in your favorite text editor to add the following text:
Additional Resources
- Workspaces — Description of the Go language workspace organization. Official documentation for the Go programming language.
2.3. Using the go Compiler Copy linkLink copied to clipboard!
To build a Go program using the command line, change to the project directory and run the go compiler as follows:
For Red Hat Enterprise Linux 7:
scl enable go-toolset-1.11 'go build -o output_file go_main_package'
$ scl enable go-toolset-1.11 'go build -o output_file go_main_package'Copy to Clipboard Copied! Toggle word wrap Toggle overflow For Red Hat Enterprise Linux 8:
go build -o output_file go_main_package
$ go build -o output_file go_main_packageCopy to Clipboard Copied! Toggle word wrap Toggle overflow
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 a main package or if multiple projects or *.go files are specified, the resulting binaries are discarded. In that case, the go build command is used to verify that the supplied projects or files can be built.
Note that you can execute any command using the scl utility on Red Hat Enterprise Linux 7, causing it to be run with the Go Toolset binaries available. This allows you to run a shell session with Go Toolset go directly available:
scl enable go-toolset-1.11 'bash'
$ scl enable go-toolset-1.11 'bash'
Example 2.2. Compiling a Go Program Using the Command Line
Assuming that you have successfully created the program hello as shown in Example 2.1, “Creating a Go Program”, compile the program:
For Red Hat Enterprise Linux 7:
scl enable go-toolset-1.11 'go build hello.go'
$ scl enable go-toolset-1.11 'go build hello.go'Copy to Clipboard Copied! Toggle word wrap Toggle overflow For Red Hat Enterprise Linux 8:
go build hello.go
$ go build hello.goCopy to Clipboard Copied! Toggle word wrap Toggle overflow
This creates a new binary file called hello in the current working directory.
2.4. Running a Go Program Copy linkLink copied to clipboard!
When go 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 run the program:
./file_name
$ ./file_name
Example 2.3. Running a Go Program on the Command Line
Assuming that you have successfully compiled the hello binary file as shown in Example 2.2, “Compiling a Go Program Using the Command Line”, 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.
$ ./hello
Hello.
Starting http server.
Go to localhost:8080/welcome
To terminate press CTRL+C.
2.5. Installing Go Projects Copy linkLink copied to clipboard!
Installing a Go project means that its executable files and libraries are compiled, and copied to appropriate directories in the Go Workspace. The go tool can then use the executable files and libraries in further projects. Dependencies of the installed project are installed, too.
To install a Go project, run the go tool:
For Red Hat Enterprise Linux 7:
scl enable go-toolset-1.11 'go install go_project'
$ scl enable go-toolset-1.11 'go install go_project'Copy to Clipboard Copied! Toggle word wrap Toggle overflow For Red Hat Enterprise Linux 8:
go install go_project
$ go install go_projectCopy to Clipboard Copied! Toggle word wrap Toggle overflow
The install command accepts the same options as the build command.
2.6. Downloading Go Projects Copy linkLink copied to clipboard!
To download a 3rd party Go project from an online source and install it, run the go tool:
For Red Hat Enterprise Linux 7:
scl enable go-toolset-1.11 'go get 3rd_party_go_project'
$ scl enable go-toolset-1.11 'go get 3rd_party_go_project'Copy to Clipboard Copied! Toggle word wrap Toggle overflow For Red Hat Enterprise Linux 8:
go get 3rd_party_go_project
$ go get 3rd_party_go_projectCopy to Clipboard Copied! Toggle word wrap Toggle overflow
For more details about the possible values of 3rd_party_go_project option, run the following command:
For Red Hat Enterprise Linux 7:
scl enable go-toolset-1.11 'go help importpath'
$ scl enable go-toolset-1.11 'go help importpath'Copy to Clipboard Copied! Toggle word wrap Toggle overflow For Red Hat Enterprise Linux 8:
go help importpath
$ go help importpathCopy to Clipboard Copied! Toggle word wrap Toggle overflow
2.7. Additional Resources Copy linkLink copied to clipboard!
A detailed description of the go compiler and its features is beyond the scope of this book. For more information, see the resources listed below.
Installed Documentation
The Go compiler
helpcommand provides information on its usage. To show the help index:For Red Hat Enterprise Linux 7:
scl enable go-toolset-1.11 'go help'
$ scl enable go-toolset-1.11 'go help'Copy to Clipboard Copied! Toggle word wrap Toggle overflow For Red Hat Enterprise Linux 8:
go help
$ go helpCopy to Clipboard Copied! Toggle word wrap Toggle overflow
The Go compiler
doccommand shows documentation for packages. To show documentation for package package_name:For Red Hat Enterprise Linux 7:
scl enable go-toolset-1.11 'go doc package_name'
$ scl enable go-toolset-1.11 'go doc package_name'Copy to Clipboard Copied! Toggle word wrap Toggle overflow To learn more about the
doccommand:scl enable go-toolset-1.11 'go help doc'
$ scl enable go-toolset-1.11 'go help doc'Copy to Clipboard Copied! Toggle word wrap Toggle overflow For Red Hat Enterprise Linux 8:
go doc package_name
$ go doc package_nameCopy to Clipboard Copied! Toggle word wrap Toggle overflow To learn more about the
doccommand:go help doc
$ go help docCopy to Clipboard Copied! Toggle word wrap Toggle overflow
Online Documentation
- Command go — Official documentation of the go compiler.
See Also
- Chapter 1, Go Toolset — An overview of Go Toolset and more information on how to install it on your system.
Chapter 3. gofmt Copy linkLink copied to clipboard!
gofmt is a code formatting tool for the Go programming language, packaged together with the go compiler.
Go Toolset is distributed with gofmt 1.11.5.
3.1. Installing gofmt Copy linkLink copied to clipboard!
In Go Toolset, gofmt is provided by the go-toolset-1.11-golang package and is automatically installed with the go-toolset-1.11 package. See Section 1.4, “Installing Go Toolset”.
3.2. Formatting Code Copy linkLink copied to clipboard!
To format all code in the code_path path, run the gofmt tool as follows:
For Red Hat Enterprise Linux 7:
scl enable go-toolset-1.11 'gofmt -w code_path'
$ scl enable go-toolset-1.11 'gofmt -w code_path'Copy to Clipboard Copied! Toggle word wrap Toggle overflow For Red Hat Enterprise Linux 8:
gofmt -w code_path
$ gofmt -w code_pathCopy to Clipboard Copied! Toggle word wrap Toggle overflow
This command will directly change code in the code_path path. When code_path is a single file, the changes apply only to the file. When code_path is a directory, all .go files in the directory are processed.
When the code_path is omitted, gofmt reads standard input instead.
To print the formatted code to standard output instead of writing to the original file, omit the -w option.
It is possible to invoke gofmt through the go compiler with the fmt command. To achieve the same results as the command above, run:
For Red Hat Enterprise Linux 7:
scl enable go-toolset-1.11 'gofmt -l -w -s code_path'
$ scl enable go-toolset-1.11 'gofmt -l -w -s code_path'Copy to Clipboard Copied! Toggle word wrap Toggle overflow For Red Hat Enterprise Linux 8:
gofmt -l -w -s code_path
$ gofmt -l -w -s code_pathCopy to Clipboard Copied! Toggle word wrap Toggle overflow
3.3. Previewing Changes to Code Copy linkLink copied to clipboard!
To preview changes done by formatting code in a given path code_path, run the gofmt tool with the -d option as follows:
For Red Hat Enterprise Linux 7:
scl enable go-toolset-1.11 'gofmt -d code_path'
$ scl enable go-toolset-1.11 'gofmt -d code_path'Copy to Clipboard Copied! Toggle word wrap Toggle overflow For Red Hat Enterprise Linux 8:
gofmt -d code_path
$ gofmt -d code_pathCopy to Clipboard Copied! Toggle word wrap Toggle overflow
The output in unified diff format is printed to standard output.
It is possible to combine both the -d and -w options.
3.4. Simplifying Code Copy linkLink copied to clipboard!
To simplify code in a given path code_path, run the gofmt tool with the -s option as follows:
For Red Hat Enterprise Linux 7:
scl enable go-toolset-1.11 'gofmt -s code_path'
$ scl enable go-toolset-1.11 'gofmt -s code_path'Copy to Clipboard Copied! Toggle word wrap Toggle overflow For Red Hat Enterprise Linux 8:
gofmt -s code_path
$ gofmt -s code_pathCopy to Clipboard Copied! Toggle word wrap Toggle overflow
The code under code_path is simplified. Use the -d option to show the differences, and use the -w option to apply the changes to the code.
3.5. Refactoring code Copy linkLink copied to clipboard!
The gofmt tool can be used to refactor code by applying arbitrary substitutions. To refactor code in a given path code_path according to a rule rewrite_rule, run the gofmt tool with the -r option as follows:
For Red Hat Enterprise Linux 7:
scl enable go-toolset-1.11 'gofmt -r rewrite_rule code_path'
$ scl enable go-toolset-1.11 'gofmt -r rewrite_rule code_path'Copy to Clipboard Copied! Toggle word wrap Toggle overflow For Red Hat Enterprise Linux 8:
gofmt -r rewrite_rule code_path
$ gofmt -r rewrite_rule code_pathCopy to Clipboard Copied! Toggle word wrap Toggle overflow
The code under code_path is refactored according to the rule rewrite_rule. Use the -d option to show the differences, and use the -w option to apply the changes to the code. The additional options must be placed after the rule rewrite_rule:
For Red Hat Enterprise Linux 7:
scl enable go-toolset-1.11 'gofmt -r rewrite_rule -d code_path'
$ scl enable go-toolset-1.11 'gofmt -r rewrite_rule -d code_path'Copy to Clipboard Copied! Toggle word wrap Toggle overflow For Red Hat Enterprise Linux 8:
gofmt -r rewrite_rule -d code_path
$ gofmt -r rewrite_rule -d code_pathCopy to Clipboard Copied! Toggle word wrap Toggle overflow
Detailed description of the rewrite rules is beyond the scope of this book. For more information, see the resources listed in Section 3.6, “Additional Resources”.
3.6. Additional Resources Copy linkLink copied to clipboard!
A detailed description of the gofmt tool and its features is beyond the scope of this book. For more information, see the resources listed below.
Online Documentation
- Command gofmt — Official documentation of the gofmt tool.
See Also
- Chapter 1, Go Toolset — An overview of Go Toolset and more information on how to install it on your system.
Chapter 4. Go Race Detector Copy linkLink copied to clipboard!
Go Toolset includes the Go race detector. The race detector is a feature of the Go standard library. The race detector must be enabled at compile time and is used at runtime.
4.1. Installing the Race Detector Copy linkLink copied to clipboard!
In Go Toolset, the race detector is provided by the go-toolset-1.11-golang-race package. To install the race detector:
yum install go-toolset-1.11-golang-race
# yum install go-toolset-1.11-golang-race
A variant of the Go standard library with the runtime race detection enabled is installed.
See Also
4.2. Using the Race Detector Copy linkLink copied to clipboard!
To use the runtime race detector for a Go project, add the -race option to the go tool commands used when manipulating the project.
For a minimal approach to using the race detector, build the project with the -race option:
scl enable go-toolset-1.11 'go build -race -o output_file go_main_package'
$ scl enable go-toolset-1.11 'go build -race -o output_file go_main_package'
When you run the resulting executable, the race detector will print warnings to the standard output when a race is detected.
The race detector has a significant runtime resource overhead.
4.3. Additional Resources Copy linkLink copied to clipboard!
A detailed description of the Go race detector and its features is beyond the scope of this book. For more information, see the resources listed below.
Online Documentation
- Data Race Detector — Official documentation of the Go race detector.
See Also
- Chapter 1, Go Toolset — An overview of Go Toolset and more information on how to install it on your system.
Chapter 5. Container Image with Go Toolset for RHEL 7 Copy linkLink copied to clipboard!
The Go Toolset is available as a container image which can be downloaded from Red Hat Container Registry.
5.1. Image Contents Copy linkLink copied to clipboard!
The devtools/go-toolset-1.11-rhel7 image provides content corresponding to the following packages:
| Component | Version | Package |
|---|---|---|
|
| 1.11.5 | go-toolset-1.11 |
5.2. Access to the Image Copy linkLink copied to clipboard!
To pull the devtools/go-toolset-1.11-rhel7 image, run the following command as root:
podman pull registry.access.redhat.com/devtools/go-toolset-1.11-rhel7
# podman pull registry.access.redhat.com/devtools/go-toolset-1.11-rhel7
5.3. Usage as Builder Image with Source-to-Image Copy linkLink copied to clipboard!
The Go Toolset container image is prepared for use as a Source-to-Image (S2I) builder image.
To do so, set the following build environment variables:
IMPORT_URL-
Set this variable to a URL specifying the location of the code. The rules for the
go getcommand option apply. INSTALL_URLSet this variable to a URL specifying the location of the package that will provide the application’s main executable file when built. The rules for the
go installcommand option apply.This variable can be omitted if the main package location is identical with the location specified by the
IMPORT_URLvariable.
Example 5.1. Building a Go Application Image Using Source-to-Image
To build the md2man package from its GitHub repository:
s2i build -e IMPORT_URL='github.com/cpuguy83/go-md2man' -e INSTALL_URL='github.com/cpuguy83/go-md2man' git://github.com/cpuguy83/go-md2man registry.access.redhat.com/devtools/go-toolset-1.11-rhel7 md2man-app
$ s2i build -e IMPORT_URL='github.com/cpuguy83/go-md2man' -e INSTALL_URL='github.com/cpuguy83/go-md2man' git://github.com/cpuguy83/go-md2man registry.access.redhat.com/devtools/go-toolset-1.11-rhel7 md2man-app
A locally available application image md2man-app is built from the repository on GitHub using the go-toolset-1.11 container image.
To fully leverage the Go Toolset as a S2I builder image, build custom images based on it, with modified S2I assemble scripts and further modifications to accomodate the particular application being built.
A detailed description of the Go Toolset usage with Source-to-Image is beyond the scope of this book. For more information about Source-to-Image, refer to the OpenShift Container Platform 3.7 Image Creation Guide, Chapter 4. S2I Requirements and Using Red Hat Software Collections Container Images, Chapter 2. Using Source-to-Image (S2I).
5.4. Additional Resources Copy linkLink copied to clipboard!
- Go Toolset 1.11 - entry in the Red Hat Container Catalog
- Using Red Hat Software Collections Container Images
Chapter 6. Changes in Go Toolset in Red Hat Developer Tools 2019.1 Copy linkLink copied to clipboard!
This chapter lists some notable changes in Go Toolset since its previous release.
6.1. Go Copy linkLink copied to clipboard!
Go has been updated from version 1.10.3 to 1.11.5.
For more information, see the Go 1.11 Release Notes.