Skip to main content

Introduction to Concurrency

Golang, or Go, is a powerful programming language developed by Google that places a strong emphasis on simplicity and productivity. One of the most notable features of Go is its support for concurrency, a property that allows multiple tasks to run independently of each other. In this article, we will explore this concept in depth, starting with the basics and working our way up to more complex topics.

What is Concurrency?

Concurrency is the execution of the multiple instruction sequences at the same time. It happens in the operating system when there are several process threads running in parallel. The running process threads do not have to be executing the same task. The concept of concurrency is closely related to but distinct from the concept of parallelism.

Goroutines

In Go, the basic units of concurrency are known as goroutines. A goroutine is a lightweight thread of execution, and the cost of creating and managing a goroutine is minimal compared to traditional threads.

Here's an example of how you can create a goroutine:

func hello() {
fmt.Println("Hello from goroutine")
}

func main() {
go hello()
fmt.Println("Hello from main")
}

In this example, go hello() starts a new goroutine which will execute the hello function. The main function then continues to execute the next line of code.

Channels

Channels are the pipes that connect concurrent goroutines. You can send values into channels from one goroutine and receive those values into another goroutine.

Here's an example of how you can use channels:

func hello(ch chan string) {
ch <- "Hello from goroutine"
}

func main() {
ch := make(chan string)
go hello(ch)
fmt.Println(<-ch)
}

In this example, we create a new channel with make(chan string). We then start a new goroutine with go hello(ch), which will send a string into the channel. In the main function, we then receive the string from the channel with <-ch and print it.

Select

The select statement is used to choose from multiple send/receive channel operations. It blocks until one of its cases can run, then it executes that case. It's a powerful feature that makes it easy to handle multiple channels in a single goroutine.

Here's an example of how you can use the select statement:

func hello(ch chan string, quit chan bool) {
for {
select {
case msg := <-ch:
fmt.Println(msg)
case <-quit:
return
}
}
}

func main() {
ch := make(chan string)
quit := make(chan bool)
go hello(ch, quit)
ch <- "Hello from main"
quit <- true
}

In this example, we create two channels: ch and quit. We then start a new goroutine with go hello(ch, quit). This goroutine enters a loop where it uses a select statement to wait for values to be sent on either ch or quit.

Conclusion

Concurrency in Go is a vast topic with a lot of depth. This article only scratches the surface, but it should give you a basic understanding of how concurrency works in Go. As you continue your journey with Go, you'll discover that understanding and effectively utilizing concurrency is essential to write efficient and performant Go programs.