I think need to close  errCh, resCh

On Thu, May 22, 2025 at 1:44 PM natxo....@gmail.com <natxo.ase...@gmail.com>
wrote:

> 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
>
> --
> You received this message because you are subscribed to the Google Groups
> "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to golang-nuts+unsubscr...@googlegroups.com.
> To view this discussion visit
> https://groups.google.com/d/msgid/golang-nuts/49fb8d6d-9db7-4f62-ab34-8eefdb6e8ad9n%40googlegroups.com
> <https://groups.google.com/d/msgid/golang-nuts/49fb8d6d-9db7-4f62-ab34-8eefdb6e8ad9n%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion visit 
https://groups.google.com/d/msgid/golang-nuts/CAN1Sb7wvx8x7T8e1mX%2B3dk27ZJ04i4jjjTGOR7s5QHYcshqERA%40mail.gmail.com.

Reply via email to