Concurrency in Go (Golang)

·

3 min read

Here's a simpler explanation of Go's concurrency features:

Go (or Golang) makes it easy to do many things at once using goroutines, channels, and the select statement. Let's break it down step by step.

1. Goroutines:

In Go, a goroutine is like a mini-task that the computer can handle. It's like having multiple helpers that can work together. Starting a goroutine is simple:

go functionName()

Just put the word "go" before the function you want to do at the same time. Imagine it's like telling your computer, "Hey, do this task while I do other things!"

For example:

func sayHello() {
    fmt.Println("Hello from a helper!")
}

func main() {
    go sayHello()
    fmt.Println("Hello from the main task!")
}

In this code, the helper says "Hello" while the main task also says "Hello." Sometimes the helper might finish first, and sometimes the main task might finish first. It's like a little race!

2. Channels:

Channels are like special tubes that help the helpers (goroutines) talk to each other. They can send and receive messages. It's like passing notes between friends.

Here's how you make a channel:

ch := make(chan int)

This creates a channel that can carry numbers. You can send and get data from it:

go func() {
    ch <- 42  // Sending 42 through the channel
}()

value := <-ch  // Getting a value from the channel
fmt.Println(value)

3. Select Statement:

The select statement is like a helper that watches multiple channels at once. It's like someone watching multiple phones for calls. When a call comes on any phone, they answer it.

For example:

select {
case msg1 := <-ch1:
    fmt.Println("Got", msg1)
case msg2 := <-ch2:
    fmt.Println("Got", msg2)
case ch3 <- 3:
    fmt.Println("Sent 3 to ch3")
default:
    fmt.Println("No messages")
}

This select helper listens to different channels. If a message comes on any channel, it does something. If many channels have messages, it picks one to answer. It's like a multitasking helper!

Quick Tips:

  • No Sharing Memory: Goroutines talk with channels, so we don't need to worry about sharing information directly. It's like talking through notes instead of fighting over toys.

  • Buffered Channels: Channels can hold messages before they're read. It's like a mailbox that can store letters.

  • Range and Close: Channels can be closed when no more messages will come. You can also use a range to read all messages like collecting all the candies.

  • Sync Package: There's a special tool called the sync package. It helps when things get more complicated, like when many helpers need to work together.

In Conclusion:

Go's way of doing many things at once is smart and easy. It's like having helpers, talking through channels, and using select to manage them. It's much simpler than old ways of doing many things together.

Thank you for reading. I encourage you to follow me on Twitter where I regularly share content about JavaScript and React, as well as contribute to open-source projects and learning golang. I am currently seeking a remote job or internship.

Twitter: https://twitter.com/Diwakar_766

GitHub: https://github.com/DIWAKARKASHYAP

Portfolio: https://diwakar-portfolio.vercel.app/