package main

import (
	"errors"
	"fmt"
	"time"
)

// section: problem
func sendResult(ch chan string, err error) {
	if err != nil {
		ch <- "error occurred"
		// Missing: return <- OOPS!
	}
	// Still executing here if err was not nil!
	ch <- "success" // BLOCKS FOREVER if receiver already read
}

// section: problem

func main() {
	ch := make(chan string, 1) // Only buffer for 1 message

	go sendResult(ch, errors.New("something went wrong"))

	result := <-ch
	fmt.Println("Got:", result)

	time.Sleep(500 * time.Millisecond)
	fmt.Println("Goroutine leaked - blocked on second send forever")
}
