The Golang programming language is very similar to the C programming language, and its purpose is to reduce complexity in program development. This language is widely used to implement web servers, applications, and container management tools. Among the tools that have been developed with this language are:
In the last few years, this language has opened its place in Iranian companies, and it is used for the development of backend services.
Note
C background is required for learning Go.
Important
The examples in this repository target Go 1.26 and make use of features introduced in that release. See the Go 1.26 features used in this repo section below for the highlights.
- History
- Variables and constants
- Calculation
- Conditions
- Loops
- Functions
- Strings
- Arrays and slices
mapstructinterface- Pointers
- Errors (including the generic
errors.AsType[E]from Go 1.26) - Concurrency and channels (
sync.WaitGroup.Go,sync.OnceValue) select- Generics (including self-referential type parameters from Go 1.26)
go modand using packages- An overview of advanced features
- Introduction to HTTP protocol
- HTTP server implementation with
net/http's pattern-based routing and structured logging vialog/slog(slog.NewMultiHandler) - Graceful shutdown with
signal.NotifyContextcancel causes - Settings management
- Metric, Log and Tracing
- Connection with the database using PostgreSQL and GORM
- Introduction to Docker and containerization
At the beginning of the course, an introduction to the Go language is made and students implement simple programs with it. Since the implementation of web servers is one of the important uses of the Go programming language, we will review the structure of the HTTP protocol and then implement a simple web server in Go. In this implementation, we try to familiarize ourselves with the structure of large programs created in Go and review details such as Configuration or Metrics, which are of great value in real systems. Finally, a MongoDB and PostgreSQL databases are added to this web server, the purpose of which is to familiarize students with database interfaces in the Go.
Using Go for design and implementing servers contains two major steps. First is about learning Go itself and another step is learning an HTTP framework (here we go with Echo). Reviewing these source codes are useful for learning Go but there aren't enough.
- Hello World
- Constants and Variables
- Calculation
- Conditions
- Loops
- Strings
- Arrays
- Slices
- Arrays and Slices
- Maps
- Structs
- Interfaces
- Pointers
- Structs with Pointers
strconv- Function with multiple-return
- Errors
- Concurrency
- Function Type
- Channels
- Pipelines
- Select
- JSON
go.mod- Packages
defervariadicregexsync.Once/sync.OnceValuepanic- UTF-8
- The
nilinterface trap - Type aliases vs. type definitions
- Self-referential generics (Go 1.26)
This repository has been refreshed to use Go 1.26 language and library features. The most notable ones — and where to find a runnable example for each — are:
| Feature | Where |
|---|---|
new(expr) builtin: pointer + initial value in one step |
22-json/main.go |
errors.AsType[E] — generic, type-safe errors.As |
16-errors/main.go |
Self-referential generic type parameters (Adder[A Adder[A]]) |
33-generics-self-referential/main.go |
slog.NewMultiHandler for log fan-out |
httpserver/main.go |
signal.NotifyContext + context.Cause for graceful shutdown |
httpserver/main.go |
The repository also benefits from earlier-but-still-modern features that are worth highlighting in class:
sync.WaitGroup.Go(func)instead ofAdd/go/defer Done(see17-concurrency/main.goand20-pipeline/main.go)sync.OnceValueinstead ofsync.Once+ sentinel field (see28-once/main.go)for range Ninstead of the C-stylefor i := 0; i < N; i++(see09-map,19-channels-1,20-pipeline,23-sharding)math/rand/v2(see21-select/main.go)net/httppattern-based routing —mux.HandleFunc("GET /hello/{username}", …)(seehttpserver/main.go)mime.ParseMediaTypefor tolerantContent-Typechecks (seehttpserver/handler/hello.go)
Go 1.26 ships a much expanded go fix with a suite of modernizer analyzers
that automatically rewrite older idioms (interface{} → any,
for i := 0; i < n; i++ → for range n, WaitGroup.Add/Done → WaitGroup.Go,
and many more). To run all of them across this module:
just modernize
# or, equivalently:
go fix ./...You can visit Go101 which contains some more advance concepts of Golang.
One of the main steps in learning new language and its best practices is reviewing written projects:
-
https://github.com/1995parham/koochooloo:
- In the first step, you need to review the project structure and find out how modules are related
- Then we continue with running the docker-compose to have the requirements
- And in the final step, we lunch the application and trying it with curl based on its swagger
- This project use zap as a logger and pass it into its modules also each module has its metrics based on otel.
-
https://github.com/1995parham/fandogh:
- This example containing the migration and how we store things on the MongoDB database.
-
https://github.com/1995parham-teaching/k1s:
- In the first step, we review the server structure. The server is stateless and only returns simple responses.
- We it on the cloud with its manifests
- using server and ingress to send requests and see how they distributed between instances
- We also see how we can mount configuration on Kubernetes with configmap.
