Cheatsheet for Golang beginners

1 - Create startup repository

See [first demo application in go]

1.1 - Setup tools locally

git config --global user.email "[email protected]"  
git config --global user.name "RobertTC32"
git config --global init.defaultbranch "main"  
git config --global pull.ff "only"  
# For windows workstations, add:  
git config --global core.autocrlf "true"  
git config --global core.editor "notepad"  
# For linux workstations, add:  
#   git config --global core.autocrlf "false"  
#   git config --global core.editor "nano"  
ssh-keygen -t ed25519 -C "[email protected]"  

1.2 - Create repository in Github/Gitea

1.3 - Use a Branching Strategy

This strategy with one shared main branch,
and multiple feature branches for work in progress:

2 - Prepare go development

2.1 - Setup golang development

go version  
go mod init RobertTC32/example-demo_hello
# "go run" needs "go.mod" file in current folder,  
# and get folder of "main.go" as parameter
go run ./src
go help

2.2 - Create automation scripts for development

make run
make build
make clean
make

3 - Develop first web applications

See [todo application which is used as go example]

3.1 - Revamp HelloWorld application

npm init
npm install tailwindcss @tailwindcss/cli
echo '@import "tailwindcss";' > src/resources/style.tailwindcss.css
# add line in html file: 
#   <link href="public/style.css" rel="stylesheet">
# add lines in in "Makefile." file: 
#   generate-twc: 
#     npx @tailwindcss/cli -i ./src/resources/style.tailwindcss.css -o ./dist/public/style.css
# add line in "scripts" section in "package.json" file: 
#   "dev": "npx @tailwindcss/cli -i ./src/resources/style.tailwindcss.css -o ./dist/public/style.css --watch"
# start npx watch process:
npm run dev 
(cd ./dist && go run ../src)  

3.2 - Develop primitive Todo application

Setup and learn configuration, logging and database middleware

   go get github.com/joho/godotenv
docker run --name mydb --rm -d -p 3306:3306 -e MARIADB_ROOT_PASSWORD=myrootpw -e MARIADB_DATABASE=demo_todo mariadb:12.2 
docker exec -it mydb mariadb -uroot -p
MariaDB> show databases;
MariaDB> use mydatabase;
MariaDB> show tables;
...
MariaDB> exit
# content of "create_todos.sql" file:
use demo_todo;
CREATE TABLE IF NOT EXISTS todos (
    id int unsigned NOT NULL AUTO_INCREMENT,
    title varchar(255) CHARACTER SET utf8 NOT NULL,
    completed boolean NOT NULL DEFAULT FALSE, 
    created_at datetime NOT NULL,
    completed_at datetime DEFAULT NULL,
    PRIMARY KEY (id)
);
# 
# content of "insert_todos.sql" file:
use demo_todo;
DELETE FROM todos;
INSERT INTO todos (title, completed, created_at, completed_at) 
  VALUES ('Bake a cake', TRUE, STR_TO_DATE('2025-02-18 15:44:04', '%Y-%m-%d %H:%i:%s'), STR_TO_DATE('2025-02-18 16:44:04', '%Y-%m-%d %H:%i:%s'));
INSERT INTO todos (title, completed, created_at, completed_at) 
  VALUES ('Feed the cat', FALSE, STR_TO_DATE('2025-02-18 15:55:04', '%Y-%m-%d %H:%i:%s'), NULL);
INSERT INTO todos (title, completed, created_at, completed_at) 
  VALUES ('Take out the trash', FALSE, STR_TO_DATE('2025-02-18 15:57:10', '%Y-%m-%d %H:%i:%s'), NULL);
SELECT * FROM todos;
go get github.com/go-sql-driver/mysql

Use configuration, logging and database in go application

Implement graceful shutdown in go application

4 - Deploy first web applications

4.1 - Containerize web applications

MAKE_TIME = $(shell date +"%FT%H:%M:%SZ")

build-oci: build
	docker build . -t $(OCI_NAME):latest --label "version=${OCI_VERSION}" --label "build=$(MAKE_TIME)"
	docker image ls | grep $(OCI_NAME) 
	docker image inspect $(OCI_NAME):latest

run-oci: 
	docker run  --name myapp --rm -p $(OCI_PORT):$(OCI_INT_PORT) -e OCI_PORT=$(OCI_PORT) \  
    -e DB_PORT=$(DB_PORT) -e DB_HOST=host.docker.internal -e DB_USER=$(DB_USER) -e DB_PASSWORD=$(DB_PASSWORD) -e DB_NAME=$(DB_NAME) \
    -e LOG_LEVEL=debug $(OCI_NAME):latest

4.2 - Deploy web applications

Saving and loading oci images to/from compressed files

There are several ways to transfer Docker images between different machines:

As beginner I wanted to directly transfer a Docker image between machines without using a registry.

save-oci:
  mkdir -p ./temp
  docker save -o temp/$(OCI_NAME).tar $(OCI_NAME):latest
  du -sh temp/$(OCI_NAME).tar
load-oci:
  docker load -i temp/$(OCI_NAME).tar
  docker image ls | grep $(OCI_NAME)

Transferring files between machines

...
Host home-testappserver
  HostName 192.168.0.94
  User myadmin

Host home-prodappserver
  HostName 192.168.0.95
  User myadmin

Deploying HelloWorld and Todo application on application servers

5 - Create documentation and diagrams

5.1 - Create development diagrams

5.2 - Create development documents


In the following "Cheatsheet for golang advanced" part,
more golang programming topics will be covered,
and the implementation of the HelloWorld and Todo application will be completed.