Hands on GoLang
Setup
go mod init github.com/kararnab/handsongo
go mod tidy
-> Install external dependencies, and remove unused ones (if any)
make server-run
or go run .
-> Starts dev server
make server-build
-> Build the server executable
Generate a random JWT Secret
node -e "console.log(require('crypto').randomBytes(32).toString('hex'))"
This script will generate a random 64 bit ASCII string, e.g. that can be used for encrypting JWT tokens.
Explanation is as follows-
node -e
tells Node.js to evaluate a script, in this case, a Javascript string
crypto
is the cryptographic module forming part of Node.js core. It is already installed as part of Node.js, no extra npm package is involved.
randomBytes()
is a function that generates cryptographically strong pseudo-random data. It will return a Buffer object.
toString()
is a method of the Buffer class that decodes the object to a string according to the specified character encoding, which, in this case, is hex, viz. hexadecimal.
- Each of the 64 characters can be 0-9 and A,B,C,D,E or F
Libraries Used
- DotEnv
go get github.com/joho/godotenv
- JWT
github.com/golang-jwt/jwt/v5 v5.0.0
- PASETO
github.com/o1egl/paseto v1.0.0
- GIN
go get -u github.com/gin-gonic/gin
- Structured Logging using
Zerolog
-> go get -u github.com/rs/zerolog/log
- Stringer
go get -u golang.org/x/tools/cmd/stringer
To Study
- Channel and Goroutine
- Transaction Isolation level in PostGRES
Popular Web Frameworks in GO
Although we can use the standard net/http package to implement APIs, it would be much easier to just take advantage of some existing web frameworks as they come loaded with features like routing, parameter binding, validation, middleware, built in ORM etc. Some of the most popular goLang web framework are-
- Gin (42.4k β
)
- Beego (25.1k β
)
- Echo (18.k β
)
- Revel (11.9k β
)
- Martini (11.1k β
)
- Fiber (8.6k β
)
- Buffalo (5.9k β
)
If you prefer a lightweight package with only routing feature, these are some of the most popular HTTP routers-
- FastHttp (13.6 β
)
- Gorilla Mux (12.9 β
)
- HttpRouter (11.9 β
)
- Chi (8.3 β
)
Database Interaction Comparisons
Database/SQL
- Very fast & straightforward
- Manual mapping SQL fields to variable
- Easy to make mistakes, not caught until runtime
GORM
- CRUD functions already implemented, very short production code
- Must learn to write queries using gorm's function
- Run slowly on high load, 3-5 times slower than standard library
SQLX
- Quite fast & easy to use, faster than GORM and near to standard library performance
- Field mapping via query text & struct tags
- Failure won't occur until runtime
- Very fast and easy to use
- Automatic code generation to native library CRUD code
- Catch SQL query errors before generating codes
- Full support Postgres. MySQL is experimental.
Minikube & kubectl commands
- Minikube (Starting up/deleting the cluster)
- kubectl (configuring the minikube cluster)
minikube start
minikube stop
minikube delete
minikube status
minikube dashboard
-> Access k8s dashboard running within the cluster
kubectl version
-> Show client and server version of k8s
kubectl logs [podname]
-> Log to console
kubectl exec -it [podname] -- bin/bash
-> Get interactive terminal
kubectl get nodes | pod | services | replicaset | deployment
[-o yaml] -> Status of different k8s component, as per etcd
kubectl describe service service_name
-> Describes the service with selector, port, endpoint etc
kubectl create | edit | delete deployment image_name
-> Create/edit/delete deployments
kubectl apply -f nginx-deployment.yaml
kubectl delete -f nginx-deployment.yaml
Docker Commands frequently used
docker pull postgres:12-alpine
-> A minimal Docker image of Postgres
docker images
docker ps -a
-> To show all the containers, whether running or not
docker ps
-> To show all running containers
docker start|stop [containerId]
docker exec -it postgres12 /bin/sh
-> Go inside postgres shell
Most frequent Git commands
Here are few of the important commands in Git
git add .
-> Adds all files to be staged for next commit
git commit --amend
-> Amends changes to the last commit, note that this changes commit SHA
git commit --amend --date="now"
-> Updates the date timestamp of the commit
git fetch -ap
-> Fetch with Append and Prune
git log branchB..branchA
-> show the commits on branchA that are not on branchB
git rebase master
-> apply any commits of current branch on top of master (chronologically)
git reset HEAD{0}
-> Soft reset to the head i.e. most recent commit of current branch
git branch -D <branch_name>
| git branch -d <branch_name>
-> Deletes branch from local (D is force, d is normal)
git push origin --delete old-branch
-> Deletes branch from remote
git tag -a v1.4 -m "my version 1.4"
> git push origin <tag_name>
-> new annotated tag identified with v1.4
Mocking, Testing and π€
For mocking, we use Mockgen.