Skip to content

About

Introduction to Go πŸ‘‹ also used in IE Course

Topics

Resources

Stars

29 stars

Watchers

1 watching

Forks

Latest commit

Β 

History

192 Commits

Folders and files

NameName
Last commit message
Last commit date
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

🐼 Learning Go πŸ€“

Learn to develop cloud-native programs with Go!

banner
GitHub Actions Workflow Status

Introduction

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.

Note

This module has zero external dependencies β€” no require block, no go.sum, nothing to go get. Everything here is the standard library, the HTTP server included. just deps fails the build if that ever stops being true.

Outline (As a separate course)

  • History
  • Variables and constants
  • Calculation
  • Conditions
  • Loops
  • Functions
  • Strings
  • Arrays and slices
  • map
  • struct
  • interface
  • 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 mod and using packages
  • An overview of advanced features
  • Introduction to HTTP protocol
  • HTTP server implementation with net/http's pattern-based routing and structured logging via log/slog (slog.NewMultiHandler)
  • Graceful shutdown with signal.NotifyContext cancel 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.

Outline (As a part of internet engineering course)

Using Go for design and implementing servers contains two major steps. First is about learning Go itself, and the second is writing an HTTP server. For the second step we stay on the standard library: this module has no external dependencies at all, and httpserver/ is built on net/http with the pattern-based routing added in Go 1.22. Learn that first β€” when you later reach for a framework, you will know exactly what it is doing for you. Reviewing these source codes are useful for learning Go but there aren't enough.

Go

  1. Hello World
  2. Constants and Variables
  3. Calculation
  4. Conditions
  5. Loops
  6. Strings
  7. Arrays
  8. Slices
  9. Arrays and Slices
  10. Maps
  11. Structs
  12. Interfaces
  13. Pointers
  14. Structs with Pointers
  15. strconv
  16. Function with multiple-return
  17. Errors
  18. Concurrency
  19. Function Type
  20. Channels
  21. Pipelines
  22. Select
  23. JSON
  24. go.mod
  25. Packages
  26. defer
  27. variadic
  28. regex
  29. sync.Once / sync.OnceValue
  30. panic
  31. UTF-8
  32. The nil interface trap
  33. Type aliases vs. type definitions
  34. Self-referential generics (Go 1.26)

Go 1.26 features used in this repo

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:

Keeping the codebase modern

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 ./...

Examples πŸ“‚

Every directory is a runnable program. Run one with just run <directory>, for example just run 05-strings.

Example What it shows
00-hello-world The first program: package main, import, and func main
01-constants-and-variables var, const, :=, and zero values
02-calculation Numeric types, conversions, and floating point surprises
03-conditions if and switch
04-loops for as the only loop. Reads n from the terminal first
05-strings A string is bytes, not characters: indexing versus range over UTF-8
06-arrays Fixed size arrays
07-slices make, len versus cap, and the reallocation append can cause
08-arrays-and-slices Slices sharing one backing array
09-map A nil map versus a maked one
10-structs Methods, the String() method, and struct comparability
11-interfaces Implicit satisfaction, type assertions, and the type switch
12-pointers Why a swap by value cannot swap
13-structs-with-pointer Pointer receivers, and satisfying an interface
14-strconv Converting between strings and numbers, with bases
15-functions-with-multiple-return Returning more than one value
16-errors error as an interface, wrapping, and errors.AsType[E] from Go 1.26
17-concurrency Goroutines and sync.WaitGroup.Go, instead of a busy loop
18-function-type Functions as values: closures, and wrapping one function in another
19-channels-1 Unbuffered versus buffered channels
19-channels-2 How a channel synchronises two goroutines
20-pipeline Chaining stages together with channels
21-select select over several channels, with math/rand/v2
22-json Struct tags, and a pointer field to tell missing from zero
23-sharding A generic sharded map, one mutex per shard
24-types Defining a new type on top of an existing one
25-defer defer for cleanup, and when it runs
26-variadic Functions taking any number of arguments
27-regex regexp, matching and replacing
28-once sync.OnceValue for one-time initialisation
29-panic A manual panic, a runtime panic, and recover
30-utf Walking a UTF-8 string byte by byte
31-interface-is-nil The typed nil: a non-nil interface holding a nil value
32-type-alias An alias versus a defined type
33-generics-self-referential Self-referential type parameters, new in Go 1.26
httpserver An HTTP server: pattern routing, graceful shutdown, layered packages
mutex sync.Mutex, with tests

Development πŸ› οΈ

just              # list every recipe
just run 09-map   # run a single example
just check        # everything the ci runs: gofmt, vet, build, test
just modernize    # apply the go 1.26 modernizer fixers

Continue your journey 🧳

You can visit Go101 which contains some more advance concepts of Golang.

Review Me

One of the main steps in learning new language and its best practices is reviewing written projects:

About

Introduction to Go πŸ‘‹ also used in IE Course

Topics

Resources

Stars

29 stars

Watchers

1 watching

Forks

Used by

Contributors

Languages