your first golang tutorial

go tutorial


  • go: 1.13.1


sudo tar -C /usr/local -xzf go1.13.1.linux-amd64.tar.gz

ll /usr/local/go

vim ~/.bashrc
export PATH=$PATH:/usr/local/go/bin

source ~/.bashrc

zsh uses env profile ~/.zshrc, bash use env profile ~/.bashrc.

check version

go version
go version go1.13.1 linux/amd64


just delete /usr/local/go


Create your workspace directory, $HOME/go.

The GOPATH environment variable specifies the location of your workspace. If no GOPATH is set, it is assumed to be $HOME/go on Unix systems.

Note that GOPATH must not be the same path as your Go installation.

issue the commands

vim .bashrc 
# for golang
export GOPATH=$HOME/go
export PATH=/usr/local/go/bin:$GOPATH:$PATH

source .bashrc

#go env -w GOPATH=$HOME/go

$ echo $GOPATH 

$ go env GOPATH

code organization

  • Go programmers typically keep all their Go code in a single workspace.
  • A workspace contains many version control repositories (managed by Git, for example).
  • Each repository contains one or more packages.
  • Each package consists of one or more Go source files in a single directory.
  • The path to a package’s directory determines its import path.

like this

    hello                          # command executable
    outyet                         # command executable
        .git/                      # Git repository metadata
            hello.go               # command source
            main.go                # command source
            main_test.go           # test source
            reverse.go             # package source
            reverse_test.go        # test source
        .git/                      # Git repository metadata
            reader.go              # package source
            writer.go              # package source
    ... (many more repositories and packages omitted) ...

Note that symbolic links should not be used to link files or directories into your workspace.

An import path is a string that uniquely identifies a package.

go example

your first program

mkdir -p $GOPATH/src/
cd $GOPATH/src/
vim hello.go


package main

import "fmt"

func main() {
    fmt.Printf("hello, world\n")

build and run

go build
hello, world

install and clean binary files

# install hello to $HOME/go/bin
go install  

# clean $HOME/go/bin/*
go clean -i

~/go/src$ go build
~/go/src$ go install

your first library

stringutil library

mkdir -p $GOPATH/src/
cd $GOPATH/src/
vim reverse.go


// Package stringutil contains utility functions for working with strings.
package stringutil

// Reverse returns its argument string reversed rune-wise left to right.
func Reverse(s string) string {
    r := []rune(s)
    for i, j := 0, len(r)-1; i < len(r)/2; i, j = i+1, j-1 {
        r[i], r[j] = r[j], r[i]
    return string(r)
package <name>

where name is the package’s default name for imports. (All files in a package must use the same name.)
executable commands must always use package main.

build library

go build
#This won't produce an output file. Instead it saves 
#the compiled package in the local build cache.

use stringutil in hello.go

package main

import (

func main() {
    fmt.Printf("hello, world\n")
    fmt.Println(stringutil.Reverse("!oG ,olleH"))

build and install

go build
go install

~/go/bin$ ./hello 
hello, world
Hello, Go!

folder structure

tree .
├── bin
│   └── hello
└── src
        └── kezunlin
            ├── hello
            │   └── hello.go
            └── stringutil
                └── reverse.go

6 directories, 3 files


You write a test by creating a file with a name ending in _test.go that contains functions named TestXXX with signature func (t *testing.T). The test framework runs each such function; if the function calls a failure function such as t.Error or t.Fail, the test is considered to have failed.

  • file name: xxx_test.go
  • function name: TextXXX
  • error: t.Error or t.Fail


package stringutil

import "testing"

func TestReverse(t *testing.T) {
    cases := []struct {
        in, want string
        {"Hello, world", "dlrow ,olleH"},
        {"Hello, 世界", "界世 ,olleH"},
        {"", ""},
    for _, c := range cases {
        got := Reverse(
        if got != c.want {
            t.Errorf("Reverse(%q) == %q, want %q",, got, c.want)

test ok

$ go test
ok 0.165s

test error

--- FAIL: TestReverse (0.00s)
    reverse_test.go:16: Reverse("Hello, 世界2") == "2界世 ,olleH", want "界世 ,olleH"
exit status 1
FAIL  0.003s    

remote packages

$ go get
$ $GOPATH/bin/hello
Hello, Go examples!

go commands

go help gopath 
go help importpath 
go help test

go build
go install 
go clean

go get # fetch,build and install



  • 20191011: created.

Author: kezunlin
Reprint policy: All articles in this blog are used except for special statements CC BY 4.0 reprint polocy. If reproduced, please indicate source kezunlin !