How to Use a Done Channel to Signal Completion

Signal task completion in Go by closing a done channel and waiting on it with a receive operation.

Use a done channel to signal completion by creating a buffered channel, sending a signal when the task finishes, and receiving it in the main goroutine to wait for termination.

package main

import "fmt"

func main() {
    done := make(chan struct{})
    
    go func() {
        // Simulate work
        fmt.Println("Working...")
        close(done) // Signal completion
    }()
    
    <-done // Wait for signal
    fmt.Println("Done")
}