Hello World
A sample go program is show here.
package main
import "fmt"
func main() {
message := greetMe("world")
fmt.Println(message)
}
func greetMe(name string) string {
return "Hello, " + name + "!"
}
Run the program as below:
$ go run hello.go
Variables
Normal Declaration:
var msg string
msg = "Hello"
Shortcut:
msg := "Hello"
Constants
const Phi = 1.618
Strings
str := "Hello"
Multiline string
str := `Multiline
string`
Numbers
Typical types
num := 3 // int
num := 3. // float64
num := 3 + 4i // complex128
num := byte('a') // byte (alias for uint8)
Other Types
var u uint = 7 // uint (unsigned)
var p float32 = 22.7 // 32-bit float
Arrays
// var numbers [5]int
numbers := [...]int{0, 0, 0, 0, 0}
Pointers
func main () {
b := *getPointer()
fmt.Println("Value is", b)
func getPointer () (myPointer *int) {
a := 234
return &a
a := new(int)
*a = 234
Pointers point to a memory location of a variable. Go is fully garbage-collected.
Type Conversion
i := 2
f := float64(i)
u := uint(i)
Slice
slice := []int{2, 3, 4}
slice := []byte("Hello")
Condition
if day == "sunday" || day == "saturday" {
rest()
} else if day == "monday" && isTired() {
groan()
} else {
work()
}
if _, err := doThing(); err != nil {
fmt.Println("Uh oh")
Switch
switch day {
case "sunday":
// cases don't "fall through" by default!
fallthrough
case "saturday":
rest()
default:
work()
}
Loop
for count := 0; count <= 10; count++ {
fmt.Println("My counter is at", count)
}
entry := []string{"Jack","John","Jones"}
for i, val := range entry {
fmt.Printf("At position %d, the character %s is present\n", i, val)
n := 0
x := 42
for n != x {
n := guess()
}
Condition
if day == "sunday" || day == "saturday" {
rest()
} else if day == "monday" && isTired() {
groan()
} else {
work()
}
if _, err := doThing(); err != nil {
fmt.Println("Uh oh")
Variable
NAME="John"
echo $NAME
echo "$NAME"
echo "${NAME}
Condition
if [[ -z "$string" ]]; then
echo "String is empty"
elif [[ -n "$string" ]]; then
echo "String is not empty"
fi
Install Qemu
#!/bin/bash
echo -e "\n update packages\n"
sudo apt update -y && sudo apt upgrade -y
sleep 5
echo -e "\n update timezone\n"
sudo timedatectl set-timezone Asia/Manila
date
sleep 3
echo -e "\n install qemu agent\n"
sudo apt install qemu-guest-agent -y
sleep 2
sudo systemctl enable qemu-guest-agent
sleep 2
sudo systemctl restart qemu-guest-agent
sleep 2
sudo systemctl status qemu-guest-agent
echo -e "\n done\n"
Install Docker
#!/bin/bash
echo -e "\n install docker dependencies\n"
sudo apt install apt-transport-https ca-certificates curl software-properties-common -y
sleep 5
echo -e "\n add GPG Key\n"
sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
sleep 5
echo -e "\n add docker repository\n"
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
sleep 5
echo -e "\n install docker-ce\n"
sudo apt install docker-ce -y
sudo systemctl restart docker
sudo systemctl enable docker
sleep 3
sudo systemctl status docker
sleep 3
echo -e "\n add user to docker group\n"
sudo usermod -aG docker ${USER}
sleep 3
echo -e "\n done\n"
Install Portainer
docker run -d \
-p 8000:8000 \
-p 9443:9443 \
--name do-portainer \
--restart=always \
-v /var/run/docker.sock:/var/run/docker.sock \
-v /opt/docker-data/portainer/data:/data \
portainer/portainer-ce:latest
Install Portainer Agent
docker run -d \
-p 9001:9001 \
--name do-portainer-agent \
--restart=always \
-v /var/run/docker.sock:/var/run/docker.sock \
-v /opt/docker-data/portainer-agent/volumes:/var/lib/docker/volumes \
portainer/agent:2.19.5