Visit https://golang.org/doc/install for details on how to install w.r.t OS flavours
In my case i am using linux OS
[root@gomachine ~]# wget https://dl.google.com/go/go1.13.4.linux-amd64.tar.gz
[root@gomachine ~]# ls -l
total 117244
-rw-r–r–. 1 root root 120054682 Nov 1 00:19 go1.13.4.linux-amd64.tar.gz
[root@gomachine ~]# tar -C /usr/local -xzf go1.13.4.linux-amd64.tar.gz
[root@gomachine ~]# ls -l /usr/local/go/
total 204
drwxr-xr-x. 2 root root 4096 Oct 31 22:56 api
-rw-r–r–. 1 root root 55389 Oct 31 22:56 AUTHORS
drwxr-xr-x. 2 root root 29 Oct 31 22:58 bin
-rw-r–r–. 1 root root 1339 Oct 31 22:56 CONTRIBUTING.md
-rw-r–r–. 1 root root 84339 Oct 31 22:56 CONTRIBUTORS
drwxr-xr-x. 8 root root 4096 Oct 31 22:56 doc
-rw-r–r–. 1 root root 5686 Oct 31 22:56 favicon.ico
drwxr-xr-x. 3 root root 18 Oct 31 22:56 lib
-rw-r–r–. 1 root root 1479 Oct 31 22:56 LICENSE
drwxr-xr-x. 13 root root 193 Oct 31 22:56 misc
-rw-r–r–. 1 root root 1303 Oct 31 22:56 PATENTS
drwxr-xr-x. 6 root root 76 Oct 31 22:58 pkg
-rw-r–r–. 1 root root 1607 Oct 31 22:56 README.md
-rw-r–r–. 1 root root 26 Oct 31 22:56 robots.txt
-rw-r–r–. 1 root root 397 Oct 31 22:56 SECURITY.md
drwxr-xr-x. 47 root root 4096 Oct 31 22:56 src
drwxr-xr-x. 23 root root 12288 Oct 31 22:56 test
-rw-r–r–. 1 root root 8 Oct 31 22:56 VERSION
[root@gomachine ~]# /usr/local/go/bin/go version
go version go1.13.4 linux/amd64
export the required PATH’s
[root@gomachine ~]# export PATH=$PATH:/usr/local/go/bin
Go possible commands
[root@gomachine ~]# go
Go is a tool for managing Go source code.Usage:
go [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 packagesUse “go help ” for more information about a command.
Additional help topics:
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 functionsUse “go help ” for more information about that topic.
go version
[root@gomachine ~]# go version
go version go1.13.4 linux/amd64
[root@gomachine ~]# export GOROOT=/usr/local/go
[root@gomachine ~]# export PATH=$PATH:$GOROOT/bin
[root@gomachine ~]# mkdir /root/golib
[root@gomachine ~]# export GOPATH=/root/golib
[root@gomachine ~]# export PATH=$PATH:$GOPATH/bin
[root@gomachine ~]# ls -l golib/
total 0
go get (add dependencies to current module and install them) in the GOPATH
[root@gomachine ~]# go get github.com/golang/example/hello
[root@gomachine ~]# ls -l golib/
total 0
drwxr-xr-x. 2 root root 19 Nov 23 02:25 bin
drwxr-xr-x. 3 root root 24 Nov 23 02:25 src
[root@gomachine ~]# ls -l golib/bin/
total 1968
-rwxr-xr-x. 1 root root 2012966 Nov 23 02:25 hello
[root@gomachine ~]# ls -l golib/src/github.com/golang/example/
total 16
drwxr-xr-x. 3 root root 67 Nov 23 02:25 appengine-hello
drwxr-xr-x. 12 root root 229 Nov 23 02:25 gotypes
drwxr-xr-x. 2 root root 22 Nov 23 02:25 hello
-rw-r–r–. 1 root root 11358 Nov 23 02:25 LICENSE
drwxr-xr-x. 2 root root 82 Nov 23 02:25 outyet
-rw-r–r–. 1 root root 2634 Nov 23 02:25 README.md
drwxr-xr-x. 2 root root 47 Nov 23 02:25 stringutil
drwxr-xr-x. 2 root root 57 Nov 23 02:25 template
[root@gomachine ~]# golib/bin/hello
Hello, Go examples!
go run (compile and run Go program)
[root@gomachine ~]# go run golib/src/github.com/golang/example/hello/hello.go
Hello, Go examples!
go build (compile packages and dependencies)
Go build just compile the executable file and move it to destination.
[root@gomachine ~]# go build golib/src/github.com/golang/example/hello/hello.go
We can remove the binary & it will get generated after you do go install
[root@gomachine ~]# rm -rf golib/bin/hello
[root@gomachine ~]# golib/bin/hello
-bash: golib/bin/hello: No such file or directory
go install (compile and install packages and dependencies)
Go install do a little more. It move the executable file to $GOPATH/bin
[root@gomachine ~]# go install golib/src/github.com/golang/example/hello/hello.go
go install: no install location for .go files listed on command line (GOBIN not set)
[root@gomachine ~]# export GOBIN=/root/golib/bin/
[root@gomachine ~]# go install golib/src/github.com/golang/example/hello/hello.go
[root@gomachine ~]# golib/bin/hello
Hello, Go examples!
[root@gomachine ~]# go build golib/src/github.com/golang/example/hello/hello.go
[root@gomachine ~]# ls -l
total 119212
-rw-r–r–. 1 root root 120054682 Nov 1 00:19 go1.13.4.linux-amd64.tar.gz
drwxr-xr-x. 4 root root 28 Nov 23 02:25 golib
-rwxr-xr-x. 1 root root 2012966 Nov 23 02:48 hello
[root@gomachine ~]# rm -rf hello
[root@gomachine ~]# date
Sat Nov 23 02:53:01 UTC 2019
[root@gomachine ~]# go build golib/src/github.com/golang/example/hello/hello.go
[root@gomachine ~]# ls -l
total 119212
-rw-r–r–. 1 root root 120054682 Nov 1 00:19 go1.13.4.linux-amd64.tar.gz
drwxr-xr-x. 4 root root 28 Nov 23 02:25 golib
-rwxr-xr-x. 1 root root 2012966 Nov 23 02:53 hello
[root@gomachine ~]# cd golib/src/github.com/golang/example/hello/
[root@gomachine hello]# ls -l
total 4
-rw-r–r–. 1 root root 706 Nov 23 02:25 hello.go
[root@gomachine hello]# go build hello.go
[root@gomachine hello]# ls -l
total 1972
-rwxr-xr-x. 1 root root 2012966 Nov 23 02:51 hello
-rw-r–r–. 1 root root 706 Nov 23 02:25 hello.go
go env (print Go environment information)
[root@gomachine ~]# go env
GO111MODULE=””
GOARCH=”amd64″
GOBIN=”/root/golib/bin/”
GOCACHE=”/root/.cache/go-build”
GOENV=”/root/.config/go/env”
GOEXE=””
GOFLAGS=””
GOHOSTARCH=”amd64″
GOHOSTOS=”linux”
[root@gomachine ~]# mkdir testing
[root@gomachine ~]# cd testing/
[root@gomachine testing]# vim test.go
package mainimport “testing”
func TestAbc(t *testing.T) {
t.Error() // to indicate test failed
}
go test (test packages)
[root@gomachine testing]# go test
? _/root/testing [no test files]
In Go, you save unit tests inside separate files with a filename ending with _test.go
[root@gomachine testing]# mv test.go sample_test.go
[root@gomachine testing]# go test
— FAIL: TestAbc (0.00s)
sample_test.go:5:
FAIL
exit status 1
FAIL _/root/testing 0.003s
Creating a new module
[root@gomachine ~]# cd golib/src/
[root@gomachine src]# pwd
/root/golib/src
[root@gomachine src]# vim hello.go
package hellofunc Hello() string {
return “Hello, world.”
}
[root@gomachine src]# vim hello_test.go
package helloimport “testing”
func TestHello(t *testing.T) {
want := “Hello, world.”
if got := Hello(); got != want {
t.Errorf(“Hello() = %q, want %q”, got, want)
}
}
[root@gomachine src]# go test
PASS
ok _/root/golib/src 0.002s
I am editing the print condition just to make fail and check what we are getting
[root@gomachine src]# vim hello.go
package hello
func Hello123() string {
return “Hello, world. hai”
}
[root@gomachine src]# go test
— FAIL: TestHello (0.00s)
hello_test.go:8: Hello123() = “Hello, world. hai”, want “Hello, world.”
FAIL
exit status 1
FAIL _/root/golib/src 0.003s
