Search⌘ K
AI Features

Solution: Write Code with a Generator Pattern

Explore how to implement the generator pattern in Go by creating a struct, generating a channel of random integers, and consuming values concurrently. This lesson guides you through writing code that efficiently manages concurrency with this pattern.

Problem breakdown

Let’s walk through the solution step by step so we can write code that uses a generator pattern. To do this, we’ll create a struct to store and print the values.

Go (1.6.2)
type struct_name struct {
val int
message string
}

Let’s create a generator function. The generator function will return a channel that holds the next value. Also, pass n as a parameter.

 ...