DevOps Buzz
Search…
Bash / Shell
Bitbucket
Distros
Elasticsearch
General
Guidelines / Standards
microk8s
Prometheus
RabbitMQ
VirtualBox
Cheat Sheet
Golang tips and tricks

Install

Manually

Download it from https://golang.org/dl/
Extract it.
1
tar -xzvf go1.14.4.linux-amd64.tar.gz
2
sudo mv go /usr/local/
Copied!
Export env vars.
vim ~/.zshrc
1
export GOROOT=/usr/local/go
2
export GOPATH=$HOME/go
3
export PATH=$GOPATH/bin:$GOROOT/bin:$PATH
Copied!

Ubuntu (apt)

1
sudo add-apt-repository ppa:longsleep/golang-backports
2
sudo apt-get update
3
sudo apt-get install golang-go
Copied!
Add to your ~/.bashrc
1
export GOPATH=$HOME/go
2
export PATH=$PATH:/usr/local/go/bin:$GOPATH/bin
Copied!

Libs / Frameworks

Cobra

GitHub - spf13/cobra: A Commander for modern Go CLI interactions
GitHub

Async

1
package main
2
3
import (
4
"fmt"
5
"net/http"
6
)
7
8
func main() {
9
// A slice of sample websites
10
urls := []string{
11
"https://www.easyjet.com/",
12
"https://www.skyscanner.de/",
13
"https://www.ryanair.com",
14
"https://wizzair.com/",
15
"https://www.swiss.com/",
16
}
17
18
c := make(chan urlStatus)
19
for _, url := range urls {
20
go checkUrl(url, c)
21
22
}
23
result := make([]urlStatus, len(urls))
24
for i, _ := range result {
25
result[i] = <-c
26
if result[i].status {
27
fmt.Println(result[i].url, "is up.")
28
} else {
29
fmt.Println(result[i].url, "is down !!")
30
}
31
}
32
33
}
34
35
//checks and prints a message if a website is up or down
36
func checkUrl(url string, c chan urlStatus) {
37
_, err := http.Get(url)
38
if err != nil {
39
// The website is down
40
c <- urlStatus{url, false}
41
} else {
42
// The website is up
43
c <- urlStatus{url, true}
44
}
45
}
46
47
type urlStatus struct {
48
url string
49
status bool
50
}
Copied!

References

Doc

1
go get golang.org/x/tools/cmd/godoc
2
godoc -http :8000
Copied!

Go $PATH

1
echo 'export PATH=$(go env GOPATH)/bin:$PATH' >> ~/.zshrc
Copied!