Skip to content

AntonioMartinezFernandez/master-go-concurrency-patterns

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

master-go-concurrency-patterns

Master Go Concurrency Patterns

Concurrency Primitives

Go Routines

Fork-Join model

Processes running concurrently on a separate thread from main.

A goroutine is a lightweight thread managed by the Go runtime.

go f(x, y, z)

starts a new goroutine running

f(x, y, z)

The evaluation of f, x, y, and z happens in the current goroutine and the execution of f happens in the new goroutine.

Goroutines run in the same address space, so access to shared memory must be synchronized. The sync package provides useful primitives, although you won't need them much in Go as there are other primitives.

Channels

Channels are FIFO queues typically used to communicate typed information between go routines.

Channels are a typed conduit through which you can send and receive values with the channel operator, <-.

  ch <- v     // Send v to channel ch
  v := <- ch  // Receive from ch, and assign value to v

(The data flows in the direction of the arrow.)

Like maps and slices, channels must be created before use:

ch := make(chan int)

By default, sends and receives block until the other side is ready. This allows goroutines to synchronize without explicit locks or condition variables.

The example code sums the numbers in a slice, distributing the work between two goroutines. Once both goroutines have completed their computation, it calculates the final result.

Select

The select statement allows selecting the first operation ready to be executed from multiple channels. It can be a signal to perform some useful task or to exit the infinite loop.

	select {
	case msgFromChannelOne := <-channelOne:
		fmt.Println(msgFromChannelOne)
	case msgFromChannelTwo := <-channelTwo:
		fmt.Println(msgFromChannelTwo)
	}

Select statement will block the execution until it receive data from a channel.

WaitGroups

To wait for goroutines to finish, we can use wait groups.

	var wg sync.WaitGroup

	for i := 1; i <= 5; i++ {
		wg.Add(1)

		go func() {
			defer wg.Done()
			doSomethingConcurrently()
		}()
	}

	wg.Wait()

Generators

A generator in computer science is a function, sometimes called a subroutine that produces values to iterate over. Those values can be the result of a computation. The iteration ends either when the generator stops producing values or when the caller terminates it explicitly (with the break keyword for instance).

Concurrency Patterns

for-select-done Pattern

The main idea of the for-select-done pattern is to use an infinite for loop to handle events from various channels using the select statement.

The select statement allows selecting the first operation ready to be executed from multiple channels. It can be a signal to perform some useful task or to exit the infinite loop.

In this pattern, the infinite for loop is usually called in a separate goroutine to avoid blocking the main thread.

Pipeline Pattern

Pipeline pattern example

The pipeline pattern allows you to easily compose sequential stages by chaining stages.

Fan-In Fan-Out Pattern

Fan-In Fan-Out pattern example

The Fan-In Fan-Out pattern allows you to execute several instances of a stage in a pipeline.

Links

About

Master Go Concurrency Patterns

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages