Urgent.News

What's breaking now, across thousands of outlets.

Tech

Go Concurrency Distilled

Article URL: https://antonz.org/go-concurrency-distilled/ Comments URL: https://news.ycombinator.com/item?id=49856988 Points: 204 # Comments: 63

Concurrency in Go revolves around two core building blocks: goroutines and channels. Goroutines are lightweight functions initiated using the go keyword. The Go runtime manages these goroutines, distributing them across operating system threads on CPU cores. Goroutines are independent of one another; the main function is also a goroutine that begins automatically upon program start.

To manage concurrent execution, Go provides synchronization primitives. A wait group (sync.WaitGroup) is utilized to pause a calling goroutine until a specified number of goroutines have finished executing. This is achieved by incrementing the wait group counter with Add(n) and decrementing it with Done(). When the counter reaches zero, the main function exits after all workers have completed their tasks.

Channels act as conduits allowing goroutines to communicate and synchronize. They function as windows where one goroutine can send a value and another can receive it. Sending a value through a channel is synchronous; the sending goroutine pauses until another goroutine receives the value. Once the value is received, the sending goroutine resumes.

When a writer goroutine closes a channel, the reader checks the channel's status to determine if all data has been sent via a second value (comma OK) during reads. While the channel is open, the reader receives the next value along with a true status; a closed channel yields a zero value and a false status. Channels can only be closed once, signifying to readers that no further data will be sent. Unused channels are automatically freed by Go's garbage collector.

Communication between goroutines is achieved via channels. A channel acts as a conduit where one goroutine can send a value and another can receive it. Sending a value synchronously blocks the sending goroutine until a receiver is present. To signify that all data has been sent, the writer closes the channel.

When a channel is no longer needed, Go's garbage collector will reclaim its resources, closed or not. The range keyword enables automatic iteration over a channel's values, halting when the channel is closed. The channel's direction can be specified to prevent accidental misuse. Channels can be either send-only, receive-only, or bidirectional. Directional channels are often parameterized in function definitions.

The time package offers utilities for time-bound operations in concurrent programs. time.After() returns a channel that receives a value after a specified timeout period, useful for handling operation timeouts. time.Timer allows scheduling future execution, with Stop() preventing expiration if the timer has not yet triggered. time.AfterFunc() simplifies timer creation by scheduling a function to run after a specified duration. Timers are essential for orchestrating timed operations in concurrent pipelines.

Written by urgent.news from Hacker News Best's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.

This story

This is one outlet's version. Read the fullest account.

Read the original at antonz.org →

More in Tech

Laravel Facades Explained: Why They Feel So Clean and Powerful

1. Hook & Problem Statement You're building a Laravel application. You need to cache some data. You write: Cache :: put ( 'user_123' , $user , 3600 ); $user = Cache :: get ( 'user_123' ); It works…

  • Laravel facades offer clean, powerful interface to complex subsystems
  • Facade Pattern simplifies complex subsystems, improves code readability
  • Facades hide complexity, enhance testability and maintainability

More from Saturday 26 September →