We are living as nomad in Japan !!

Install Go

Go

Table of contents

Download

Download the following URL.

https://golang.org/dl/

Install

For Mac OS

After downloading a package file, open the package file you downloaded and follow the prompts to install Go.

The package installs the Go distribution to "/usr/local/go". The package should put the /usr/local/go/bin directory in your PATH environment variable. You may need to restart any open Terminal sessions for the change to take effect.

Verify that you've installed Go by opening a command prompt and typing the following command.

Terminal

> go version

For source

Install Go-Bootstrap

In order to install Go, it must be built by Go itself. At first, you need to install bootstrap.

If you already have one, please skip to installing.

Terminal

> mkdir -p /usr/local/src/go
> cd /usr/local/src/go
> wget https://dl.google.com/go/go1.4-bootstrap-20171003.tar.gz
> tar xvfz go1.4-bootstrap-20171003.tar.gz
> mv go go_1_4_bootstrap
> cd go_1_4_bootstrap/src
> ./make.bash
...
Installed Go for darwin/amd64 in /usr/local/src/go/go_1_4_bootstrap
Installed commands in /usr/local/src/go/go_1_4_bootstrap/bin

Install the latest version of Go

Terminal

> mkdir -p /usr/local/src/go
> cd /usr/local/src/go

// When the latest version is 1.15.
> wget https://golang.org/dl/go1.15.src.tar.gz

> tar xvfz go1.15.src.tar.gz
> mv go 1_15
> cd 1_15/src
> GOROOT_BOOTSTRAP="/usr/local/src/go/go_1_4_bootstrap" ./make.bash
...
Installed Go for darwin/amd64 in /usr/local/src/go/1_15
Installed commands in /usr/local/src/go/1_15/bin

Setting environment variables

Terminal

// If you are using zsh.
% vim ~/.zprofile
// If you are using bash.
$ vim ~/.bash_profile

profile

...
# Go
GO_HOME=/usr/local/src/go/1_15
GOPATH=/Users/noknow/dev/go/workplace
GOBIN=$GOPATH/bin
GOROOT=$GO_HOME
export GOPATH
export GOBIN
export GOROOT

# PATH
export PATH=$GO_HOME/bin:$PATH
...

Output "Hello World"

Let's output "Hello World" as a sample code.

In this case, Go's workspace would be "/usr/local/go/workplace".

Go needs to create the following directroies.

  • bin: binary files.
  • pkg: object files.
  • src: sorce files.

Terminal

span class="cmd">$ sudo mkdir $GOPATH
span class="cmd">$ sudo chown noknow:noknow_gr $GOPATH
span class="cmd">$ cd $GOPATH
span class="cmd">$ pwd
/usr/local/go/workplace
span class="cmd">$ mkdir src pkg bin

Next, create a new project. This time, I'll create "project1".

Go can manage multiple packages and projects in one workplace.

Terminal

$ mkdir $GOPATH/src/project1

And then, create the main.go file.

Terminal

$ cd $GOPATH/src/project1
$ vim main.go

main.go

package main

import (
    "fmt"
)

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

Build the source files and then execute.

Terminal

$ go install project1
$ $GOPATH/bin/project1
Hello World

It was output "Hello World"!

Related articles