https://github.com/anazcodes/go-concurrency
Go lang concurrency practice with Go routines, Channels, Wait Groups and Select statement.
https://github.com/anazcodes/go-concurrency
concurrency golang
Last synced: 11 months ago
JSON representation
Go lang concurrency practice with Go routines, Channels, Wait Groups and Select statement.
- Host: GitHub
- URL: https://github.com/anazcodes/go-concurrency
- Owner: anazcodes
- Created: 2023-05-31T08:22:50.000Z (about 3 years ago)
- Default Branch: main
- Last Pushed: 2023-10-16T07:35:26.000Z (over 2 years ago)
- Last Synced: 2025-01-29T07:43:00.303Z (over 1 year ago)
- Topics: concurrency, golang
- Language: Go
- Homepage:
- Size: 18.6 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Go-concurrency patterns and practices
This repository contains Go-concurrency patterns and practices.
It contains different implementations using channels, switch, go-routines .
## Why concurrency supported in go ?
Look around you. What do you see?
Do you see a single-stepping world doing one thing at a time?
Or do you see a complex world of interacting, independently behaving pieces?
That's why. Sequential processing on its own does not model the world's behavior.
## What is concurrency?
Concurrency is the composition of independently executing computations.
Concurrency is a way to structure software, particularly as a way to write clean code that interacts well with the real world.
## Concurrency is not parallelism
Concurrency is not parallelism, although it enables parallelism.
If you have only one processor, your program can still be concurrent but it cannot be parallel.
On the other hand, a well-written concurrent program might run efficiently in parallel on a multiprocessor. That property could be important...
## What is a goroutine?
It's an independently executing function, launched by a go statement.
It has its own call stack, which grows and shrinks as required.
It's very cheap. It's practical to have thousands, even hundreds of thousands of goroutines.
It's not a thread.
There might be only one thread in a program with thousands of goroutines.
Instead, goroutines are multiplexed dynamically onto threads as needed to keep all the goroutines running.
But if you think of it as a very cheap thread, you won't be far off.
## Channels
A channel in Go provides a connection between two goroutines, allowing them to communicate.
Receiving from a channel.
The "arrow" indicates the direction of data flow.
value = <-c
c <- "msg"
- A sender and receiver must both be ready to play their part in the communication. Otherwise we wait until they are.
Thus channels both communicate and synchronize.
- buffer: Buffering removes synchronization.
### The Go approach
Don't communicate by sharing memory, share memory by communicating.