Getting started with go-toolset

Getting started with go-toolset

One of the new software collections we’ve introduced this fall is for Go, the programming language that aims to make it easy to build simple, reliable, and efficient software. Go is a compiled, statically typed language in the C/C++ tradition with garbage collection, concurrent programming support, and memory safety features.

In go-toolset-7, we’re including everything you need to start programming in Go on Red Hat Enterprise Linux 7, in the familiar format of software collections. In this release, we’re shipping golang as a Tech Preview. (NOTE: The “-7” in our toolset name is to sync with the other collections now being released, devtoolset-7, rust-toolset-7, and llvm-toolset-7.)

Enabling the go-toolset repositories

Before we start let’s settle on the baseline. You will need a RHEL 7 system if you don’t have one you can sign up for free at Red Hat Developer Program and get free RHEL 7 for development purposes here. And then install it on a bare metal machine or in VM on VM host of your choosing.

With the RHEL 7 installation ready, we will need to obtain and enable go-toolset subscription/repository:

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

Now you should see rhel-7-server-devtools-rpms as an enabled repo in yum repo list. Something similar to:

[root@localhost ~]# yum repolist enabled
Loaded plugins: product-id, search-disabled-repos, subscription-manager
repo id status

rhel-7-server-devtools-rpms/x86_64 137

rhel-7-server-rpms/7Server/x86_64 17,249

rhel-7-server-rt-beta-rpms/x86_64 15

rhel-7-server-rt-rpms/7Server/x86_64 233

repolist: 17,634

Everything you need to grow your career.

With your free Red Hat Developer program membership, unlock our library of cheat sheets and ebooks on next-generation application development.

SIGN UP

Let’s install the go-toolset

yum install go-toolset-7

This is not enough if you want to run the integrated Go compiler test-suites. You will need two additional packages go-toolset-7-golang-tests and.go-toolset-7-golang-misc. These are not installed by default to save on the installed footprint, as they are not needed in regular Go use.

To use the go-toolset, you have to enable it using the scl tool as it is backed by the software collections technology, like the developer toolset for gcc&co.

scl enable go-toolset-7 go version

Let’s get some Go project and “install” it.

scl enable go-toolset-7 "GOPATH=`realpath ~/go` go get -d github.com/cpuguy83/go-md2man"

We have just downloaded the sources (using -d) for go package with all its dependencies, into the src dir under your GOPATH. As we used option -d, we need now to install/build the binaries using;

scl enable go-toolset-7 "GOPATH=`realpath ~/go` go install github.com/cpuguy83/go-md2man"

now you can run the binary which got installed under ~/go/bin/.

~/go/bin/go-md2man --help
Usage of /home/user/go/bin/go-md2man:
 -in string
        Path to file to be processed (default: stdin)
-out string
        Path to output processed file (default: stdout)

If you plan on using upstream go projects installed in your GOPATH, it is a good idea to add $GOPATH/bin on to your system PATH variable.

Note: Red Hat does not support any packages/projects obtained using the “go get” command in any way.

See also:

  1. https://golang.org/cmd/go/#hdr-Download_and_install_packages_and_dependencies
  2. https://golang.org/cmd/go/#hdr-Compile_and_install_packages_and_dependencies
  3. https://golang.org/cmd/go/#hdr-Test_packages

Kick starting your (“Hello World”) project

Let’s create a directory for your project to live in:

mkdir $YOUR_MAIN_GOPATH/src/hello/
Note: If you are not sure about your GOPATH execute "scl enable go-toolset-7 go env" and look for GOPATH in output, your “main” GOPATH is the first path before: i.e. if you see,GOPATH=/home/usr/go:/root/go your GOPATH is /home/usr/goor you can use the standard location as ~/go.

Afterwards, create the hello.go file using your favorite text editor, in the just created directory with the following content:

package main
import (
  "fmt"
  "net/http"
)

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

  fmt.Fprintf(w, "Welcome to the Go toolset.")

}

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)
}

Now you can build and run the hello, project:

scl enable go-toolset-7 go build hello/hello

This will place the built binary into your current working directory. Of course, you can also only “install” it using install command and run it from bin directory in your GOPATH.

See also:

  1. Official go-toolset documentation
  2. https://golang.org/cmd/go/#hdr-GOPATH_environment_variable

Whether you are new to Linux or have experience, downloading this cheat sheet can assist you when encountering tasks you haven’t done lately.

 

Join Red Hat Developer (it’s free) and get access to software, cheat sheets, books, and more.

Share