You are viewing a free preview of this lesson.
Subscribe to unlock all 10 lessons in this course and every other course on LearningBro.
Concurrency is one of Go's greatest strengths. Go was designed from the ground up to make concurrent programming simple and efficient. The key primitives are goroutines (lightweight threads) and channels (typed communication pipes).
A goroutine is a lightweight thread managed by the Go runtime:
func sayHello() {
fmt.Println("Hello from goroutine!")
}
func main() {
go sayHello() // launch a goroutine
time.Sleep(time.Second) // wait for it to finish (crude approach)
}
| Property | Description |
|---|---|
| Cost | ~2 KB initial stack (grows as needed) — much cheaper than OS threads |
| Scheduling | Managed by the Go runtime, not the OS |
| Scalability | Easily run millions of goroutines |
| Launch syntax | go functionCall() |
| Return values | Cannot return values directly — use channels |
go func() {
fmt.Println("Anonymous goroutine")
}()
go func(msg string) {
fmt.Println(msg)
}("Hello")
Channels are typed conduits for communication between goroutines:
ch := make(chan string) // unbuffered channel of strings
go func() {
ch <- "hello" // send to channel
}()
Subscribe to continue reading
Get full access to this lesson and all 10 lessons in this course.