Go installation, environment variables, and commands

Install go on my Mac and check for simple operation. We will review the role of GOROOT and GOPATH, go commands (env, get, build, run, mod), and how to use GoLand.

TOC

Install

Go binaries can be downloaded from the following page. Downloads – The Go Programming Language

Here, we will use Homebrew to install go on a Mac.

brew install go

After installation, verify that the go command can be executed.

$ go
Go is a tool for managing Go source code.

Usage:

        go <command> [arguments]

The commands are:

        bug         start a bug report
        build       compile packages and dependencies
        clean       remove object files and cached files
        doc         show documentation for package or symbol
        env         print Go environment information
        fix         update packages to use new APIs
        fmt         gofmt (reformat) package sources
        generate    generate Go files by processing source
        get         add dependencies to current module and install them
        install     compile and install packages and dependencies
        list        list packages or modules
        mod         module maintenance
        run         compile and run Go program
        test        test packages
        tool        run specified go tool
        version     print Go version
        vet         report likely mistakes in packages

Use "go help <command>" for more information about a command.

Additional help topics:

        buildconstraint build constraints
        buildmode       build modes
        c               calling between Go and C
        cache           build and test caching
        environment     environment variables
        filetype        file types
        go.mod          the go.mod file
        gopath          GOPATH environment variable
        gopath-get      legacy GOPATH go get
        goproxy         module proxy protocol
        importpath      import path syntax
        modules         modules, module versions, and more
        module-get      module-aware go get
        module-auth     module authentication using go.sum
        module-private  module configuration for non-public modules
        packages        package lists and patterns
        testflag        testing flags
        testfunc        testing functions

Use "go help <topic>" for more information about that topic.

Environment variable ( go env )

go env to check about go’s environment variables.

GOROOT

The location where Go was installed can be found at GOROOT.

$ go env | grep GOROOT
GOROOT="/usr/local/Cellar/go/1.15.6/libexec"
$ ls -l /usr/local/Cellar/go/1.15.6/libexec
total 248
-rw-r--r--    1 xxx  staff   1339 Dec  4 02:32 CONTRIBUTING.md
-rw-r--r--    1 xxx  staff  95475 Dec  4 02:32 CONTRIBUTORS
-rw-r--r--    1 xxx  staff   1303 Dec  4 02:32 PATENTS
-rw-r--r--    1 xxx  staff    397 Dec  4 02:32 SECURITY.md
-rw-r--r--    1 xxx  staff      8 Dec  4 02:32 VERSION
drwxr-xr-x   21 xxx  staff    672 Dec  4 02:32 api
drwxr-xr-x    5 xxx  staff    160 Dec  4 02:32 bin
drwxr-xr-x   45 xxx  staff   1440 Dec  4 02:32 doc
-rw-r--r--    1 xxx  staff   5686 Dec  4 02:32 favicon.ico
drwxr-xr-x    3 xxx  staff     96 Dec  4 02:32 lib
drwxr-xr-x   14 xxx  staff    448 Dec  4 02:32 misc
drwxr-xr-x    6 xxx  staff    192 Dec  4 02:32 pkg
-rw-r--r--    1 xxx  staff     26 Dec  4 02:32 robots.txt
drwxr-xr-x   69 xxx  staff   2208 Dec  4 02:32 src
drwxr-xr-x  328 xxx  staff  10496 Dec  4 02:32 test

The go command and other commands are stored under the bin folder.

$ ls -l /usr/local/Cellar/go/1.15.6/libexec/bin/
total 65864
-rwxr-xr-x  1 xxx  staff  14125324 Dec  4 02:32 go
-rwxr-xr-x  1 xxx  staff  16145836 Dec  4 02:32 godoc
-rwxr-xr-x  1 xxx  staff   3447872 Dec  4 02:32 gofmt

GOPATH

GOPATH is affected when deciding where to store packages obtained from outside.

$ go env | grep GOPATH
GOPATH="/Users/xxx/go"

As an example, let’s download Echo.

$ go get github.com/labstack/echo/...
$ tree -L 1 /Users/xxx/go
/Users/xxx/go
├── pkg
└── src

Under src Package echo and Dependency Packages of echo are stored under the src directory.

$ tree -L 3 /Users/xxx/go/src
/Users/xxx/go/src
├── github.com
│   ├── dgrijalva
│   │   └── jwt-go
│   ├── labstack
│   │   ├── echo
│   │   └── gommon
│   ├── mattn
│   │   ├── go-colorable
│   │   └── go-isatty
│   └── valyala
│       └── fasttemplate
└── golang.org
    └── x
        ├── crypto
        ├── net
        ├── sys
        ├── text
        └── time

The package object after the build is stored under pkg.

$ tree -L 4 /Users/xxx/go/pkg
/Users/xxx/go/pkg
└── darwin_amd64
    └── github.com
        └── labstack
            ├── echo
            └── echo.a

GoLand Settings

Open [GoLand] – [Preferences].

GoLand Settings

This is the GOROOT settings screen.

GoLand Settings

This is the GOPATH settings screen.

GoLand Settings

If configured correctly, the GO SDK and GOPATH if correctly configured.

Confirmation of Hello World

Create main.go file and describe the following process.

package main

import "fmt"

func main() {
  fmt.Println("Hello world")
}

go build

go build to generate an executable binary.

$ go build main.go   
$ ls -l
total 4200
-rwxr-xr-x  1 xxx  staff  2142872  Dec  4 02:32 main
-rw-r--r--  1 xxx  staff       73  Dec  4 02:32 main.go

Try to run the generated executable binary.

$ ./main 
Hello world

go run

go run can be used to build and run at the same time.

$ go run main.go   
Hello world

Run and debug in GoLand

First, check the normal execution.

Run and debug in GoLand

Click on the arrow to execute.

Run and debug in GoLand

Run debugging with breakpoints in place.

Run and debug in GoLand

The process stopped at the breakpoint.

Module management by go mod

go mod is now available in Go 1.11 and above. Dependent modules can be managed in go.mod and go.sum files.

The following subcommands are available in go mod.

$ go help mod
Go mod provides access to operations on modules.

Note that support for modules is built into all the go commands,
not just 'go mod'. For example, day-to-day adding, removing, upgrading,
and downgrading of dependencies should be done using 'go get'.
See 'go help modules' for an overview of module functionality.

Usage:

        go mod <command> [arguments]

The commands are:

        download    download modules to local cache
        edit        edit go.mod from tools or scripts
        graph       print module requirement graph
        init        initialize new module in current directory
        tidy        add missing and remove unused modules
        vendor      make vendored copy of dependencies
        verify      verify dependencies have expected content
        why         explain why packages or modules are needed

Use "go help mod <command>" for more information about a command.

go mod init

Initialize with go mod init.

$ go mod init example.com/xxx

The go.mod file has been created.

$ cat go.mod 
module example.com/xxx

go 1.15

go mod tidy

The following process is described in main.go.

package main

import (
        "net/http"

        "github.com/labstack/echo/v4"
)

func main() {
        // Echo instance
        e := echo.New()

        // Routes
        e.GET("/", hello)

        // Start server
        e.Logger.Fatal(e.Start(":1323"))
}

// Handler
func hello(c echo.Context) error {
        return c.String(http.StatusOK, "Hello, World!")
}

We use the Echo package.

go mod tidy to automatically install dependent modules and remove unused dependent modules.

$ go mod tidy
go: finding module for package github.com/labstack/echo/v4
go: downloading github.com/labstack/echo v1.4.4
go: downloading github.com/labstack/echo/v4 v4.1.17
go: downloading github.com/labstack/echo v3.3.10+incompatible
go: found github.com/labstack/echo/v4 in github.com/labstack/echo/v4 v4.1.17
go: downloading github.com/labstack/gommon v0.3.0
  (省略)
  module example.com/xxx

  go 1.15
+ require github.com/labstack/echo/v4 v4.1.17

Enabling Modules in GoLand

Open [GoLand] – [Preferences].

Enabling Modules in GoLand

Check Enable Go modules integration.

Enabling Modules in GoLand

Go Modules has been recognized.

go get and go install

Starting with go’s 1.16, the use of go get and go install is easier to understand.

go get is used to edit go.mod. go install is used for global installation of tools.

How to use go install

Available in the following formats

go install example.com/cmd@v1.0.0

The version specification is required. To specify the latest version, add @latest.

GOPATH is set, the executable binary is installed under $GOPATH/bin.

$ go install github.com/tsenart/vegeta@latest
go: downloading github.com/tsenart/vegeta v1.2.0
  (略)
$ 
$ 
$ ls $GOPATH/bin
vegeta
Let's share this post !
TOC