August 2021
Beginner
112 pages
1h 23m
English
| Puzzle 18 | A Job to Do |
| | package main |
| | |
| | import ( |
| | "fmt" |
| | ) |
| | |
| | type Job struct { |
| | State string |
| | done chan struct{} |
| | } |
| | |
| | func (j *Job) Wait() { |
| | <-j.done |
| | } |
| | |
| | func (j *Job) Done() { |
| | j.State = "done" |
| | close(j.done) |
| | } |
| | |
| | func main() { |
| | ch := make(chan Job) |
| | go func() { |
| | j := <-ch |
| | j.Done() |
| | }() |
| | |
| | job := Job{"ready", make(chan struct{})} |
| | ch <- job |
| | job.Wait() |
| | fmt.Println(job.State) |
| | } |
Guess the Output | |
|---|---|
|
|
Try to guess what the output is before moving to the next page. |
This code will print: ...
Read now
Unlock full access