Puzzle 18 Explanation: Go Job
Understand how structures work in Go.
We'll cover the following...
We'll cover the following...
Try it yourself
Try executing the code below to see the result for yourself.
Go (1.16.5)
package mainimport ("fmt")type Job struct {State stringdone 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 := <-chj.Done()}()job := Job{"ready", make(chan struct{})}ch <- jobjob.Wait()fmt.Println(job.State)}
Explanation
At first glance, the code looks fine. We’re using a pointer receiver in the Job
struct ...