go tutorial
versions:
- go: 1.13.1
install
1 | wget https://dl.google.com/go/go1.13.1.linux-amd64.tar.gz |
zsh uses env profile
~/.zshrc
, bash use env profile~/.bashrc
.
check version
1 | go version |
uninstall
just delete /usr/local/go
set GOPATH
Create your workspace directory, $HOME/go
.
The
GOPATH
environment variable specifies the location of your workspace. If noGOPATH
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
1 | vim .bashrc |
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
bin/
hello # command executable
outyet # command executable
src/
github.com/golang/example/
.git/ # Git repository metadata
hello/
hello.go # command source
outyet/
main.go # command source
main_test.go # test source
stringutil/
reverse.go # package source
reverse_test.go # test source
golang.org/x/image/
.git/ # Git repository metadata
bmp/
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
1 | mkdir -p $GOPATH/src/github.com/kezunlin/hello |
hello.go
1 | package main |
build and run
1 | go build |
install and clean binary files
1 | # install hello to $HOME/go/bin |
~/go/src$
go build github.com/kezunlin/hello/
~/go/src$go install github.com/kezunlin/hello/
your first library
stringutil library
1 | mkdir -p $GOPATH/src/github.com/kezunlin/stringutil |
reverse.go
1 | // Package stringutil contains utility functions for working with strings. |
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 usepackage main
.
build library
1 | go build github.com/kezunlin/stringutil |
use stringutil
in hello.go
1 | package main |
build and install
1 | go build github.com/kezunlin/hello |
folder structure
1 | tree . |
testing
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
ort.Fail
reverse_test.go
1 | package stringutil |
test ok
1 | $ go test github.com/kezunlin/stringutil |
test error
--- FAIL: TestReverse (0.00s)
reverse_test.go:16: Reverse("Hello, 世界2") == "2界世 ,olleH", want "界世 ,olleH"
FAIL
exit status 1
FAIL github.com/kezunlin/stringutil 0.003s
remote packages
1 | $ go get github.com/golang/example/hello |
go commands
1 | go help gopath |
Reference
History
- 20191011: created.