Go
Go (often referred to as golang) is a free and open source programming language created at Google in 2007 by Robert Griesemer, Rob Pike, and Ken Thompson. It is a compiled, statically typed language in the tradition of Algol and C, with garbage collection, limited structural typing, memory safety features and CSP-style concurrent programming features added. (Wikipedia)
Installation
After installation set the environment variable GOPATH
to a local directory. Maybe add the binary directory of Go to the PATH:
export PATH=${PATH}:${GOPATH}/bin
Arch Linux
Install the go package.
Debian
Further information: Go Wiki - Ubuntu
Using snap (list channels by snap info go
)
snap install go --channel 1.11/stable
Using APT
apt-get install golang-1.10-go
ln -s /usr/lib/go-1.10/bin/go /usr/bin/go
export GOROOT=/usr/lib/go-1.10
Manual download
Download and unzip go
wget https://storage.googleapis.com/golang/go1.11.4.linux-amd64.tar.gz
tar -xvf go1.11.4.linux-amd64.tar.gz
sudo mv go /usr/local
Set the root and add it to the path
export GOROOT=/opt/go
export PATH=${PATH}:${GOROOT}/bin
Example
Hello World program
hello-world.go
package main
import "fmt"
func main() {
fmt.Println("Hello World")
}
Run
$ go run hello-world.go
Hello World
Build and execute
$ go build hello-world.go
$ ./hello-world
Hello World
Basics
Struct
Define a struct
import "time"
type Photographer struct {
Id int
Name string
Since time.Time
Active bool
}
type Photographers []Photographer
Use it
var photographers Photographers
p := Photographer{Name: "Tom"}
photographers = append(photographers, p)
Convert
String to byte array
[]byte("my-string")
Byte array to String
string(myByteArray[:])
Advanced
Listen to HTTP/REST and gRPC
import (
"net"
"net/http"
"google.golang.org/grpc"
"google.golang.org/grpc/reflection"
)
func main() {
errRPC := make(chan error)
errHTTP := make(chan error)
go listenRPC(errRPC)
go listenHTTP(errHTTP)
select {
case err := <-errRPC:
log.Fatalf("Exited rpc with error: %s", err.Error())
case err := <-errHTTP:
log.Fatalf("Exited http with error: %s", err.Error())
}
}
func listenRPC(errChan chan error) {
lis, err := net.Listen("tcp", ":50051")
if err != nil {
errChan <- err
return
}
s := grpc.NewServer()
pb.RegisterTelegramBotApiServer(s, &server{})
reflection.Register(s) // Register reflection service on gRPC server.
errChan <- s.Serve(lis)
}
func listenHTTP(errchan chan error) {
mux := http.NewServeMux()
mux.Handle("/ready", http.HandlerFunc(readinessProbeHandler))
mux.Handle("/healthz", http.HandlerFunc(livenessProbeHandler))
errchan <- http.ListenAndServe(":8080", mux)
}
Tools
Build
Cross compile, here with Linux and disabled usage of C code by Go packages (CGO_ENABLED=0)
CGO_ENABLED=0 GOOS=linux go build -o <destination-file> .
Get
Download all depdencies
go get -d -t ./...
Optional parameters
-d
: download only without no install-t
: download test dependencies)
List
go list -f '{{ join .Imports "\n" }}' github.com/gomodule/redigo/redi
Tips
List dependencies of a package
see #List
Clean the cache
go clean -cache
Dockerfile
see Dockerfiles