If you have a single goroutine doing something and want to wait for it to finish before proceeding then you can use example below.


Example


package main

import (
"fmt"
"time"
)

func main() {
ch := make(chan bool)

go paint(ch)

// Acquire lock by waiting for channel to send something.
<- ch

fmt.Println("done")
}

func paint(ch chan <- bool) {
fmt.Println("started painting")

time.Sleep(2 * time.Second)

fmt.Println("finished painting")

// Instruct to release lock by sending a message.
ch <- true
}

started painting
finished painting
done