hi,
I do not seem to to get this one to not panic, and do not understand why yet.
This code gets the names of files using shell globbing , so go run testchannels.go dir/*
This gets the name and size info of all 1000 files in the dire, but panics at the end with a deadlock:
fatal error: all goroutines are asleep - deadlock!
goroutine 1 [select]:
main.run({0xc000118018, 0x3e8, 0x57f308?})
    /pathtestchannels.go:44 +0x278
main.main()
    /path/testchannels.go:15 +0x66
exit status 2
I do not seem to be able to find my mistake. Any hints appreciated ;-).
package main
import (
    "flag"
    "fmt"
    "os"
    "sync"
)
func main() {
    flag.Parse()
    files := flag.Args()
    run(files)
}
func run(files []string) {
    errCh := make(chan error, 10)
    resCh := make(chan string, 10)
    doneCh := make(chan struct{})
    wg := sync.WaitGroup{}
    for _, file := range files {
        wg.Add(1)
        go func(file string) {
            defer wg.Done()
            f, err := os.Stat(file)
            if err != nil {
                errCh <- fmt.Errorf("could not stat %s: %w\n", file, err)
            }
            resCh <- fmt.Sprintf("name: %s\t size: %d", f.Name(), f.Size())
        }(file)
    }
    go func() {
        wg.Wait()
        close(doneCh)
    }()
    for {
        select {
        case err := <-errCh:
            fmt.Println(err)
        case data := <-resCh:
            fmt.Println("from result channel: ", data)
        }
    }
}
regards,
Natxo