hi,
I did not close the other channels, but adding this to the select code
seems to fix it:
for {
select {
case err := <-errCh:
fmt.Println(err)
case data := <-resCh:
fmt.Println("from result channel: ", data)
case <-doneCh:
fmt.Printf("processed %d files, done now\n",
len(files))
os.Exit(0)
}
If I remove the os.Exit(0) call, then I keep getting the
processed 1000 files, done now
processed 1000 files, done now
processed 1000 files, done now
...
until I interrupt. the process with ctrl-c. With the os.Exit() It finishes
with the proper output, as far as I can see.
I need to understand this better, will get there xp.
On Thu, May 22, 2025 at 10:37 AM thatipelli santhosh <
[email protected]> wrote:
> I think need to close errCh, resCh
>
> On Thu, May 22, 2025 at 1:44 PM [email protected] <
> [email protected]> 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 [email protected].
>> 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>
>> .
>>
>
--
--
Groeten,
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 [email protected].
To view this discussion visit
https://groups.google.com/d/msgid/golang-nuts/CAHBEJzUzJsWBRtHREw%2B5OvQ%2BmaigdAz0A9%2BJDACMYuVGD9JmRw%40mail.gmail.com.