Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Trigger a channel inside the loop where the the channel is consumed

How to rigger a channel inside the loop where the same channel is consumed. Below is a sample code that does not work. How is this achievable?

https://go.dev/play/p/o5ZhNfw4IFu

package main

import (
    "context"
    "fmt"
    "time"
)

func main() {
    ch1 := make(chan struct{})
    ch2 := make(chan struct{})
    defer close(ch1)
    defer close(ch2)

    ctx, cancel := context.WithTimeout(context.Background(), time.Second*3)
    defer cancel()

    go func() {
        time.Sleep(time.Second * 1)
        ch1 <- struct{}{}
    }()

loop:
    for {
        select {
        case <-ctx.Done():
            fmt.Println("timeout")
            break loop
        case <-ch1:
            fmt.Println("ch1")
            ch2 <- struct{}{} // This here does not work!
        case <-ch2:
            fmt.Println("ch2")
        }
    }

}

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

1. send data to ch2 inside goroutine

package main

import (
    "context"
    "fmt"
    "time"
)

func main() {
    ch1 := make(chan struct{})
    ch2 := make(chan struct{})
    defer close(ch1)
    defer close(ch2)

    ctx, cancel := context.WithTimeout(context.Background(), time.Second*3)
    defer cancel()

    go func() {
        time.Sleep(time.Second * 1)
        ch1 <- struct{}{}
    }()

loop:
    for {
        select {
        case <-ctx.Done():
            fmt.Println("timeout")
            break loop
        case <-ch1:
            fmt.Println("ch1")
            go func() {
                ch2 <- struct{}{}
            }()
        case <-ch2:
            fmt.Println("ch2")
        }
    }

}

or

2. make ch2 buffered

package main

import (
    "context"
    "fmt"
    "time"
)

func main() {
    ch1 := make(chan struct{})
    ch2 := make(chan struct{}, 1)
    defer close(ch1)
    defer close(ch2)

    ctx, cancel := context.WithTimeout(context.Background(), time.Second*3)
    defer cancel()

    go func() {
        time.Sleep(time.Second * 1)
        ch1 <- struct{}{}
    }()

loop:
    for {
        select {
        case <-ctx.Done():
            fmt.Println("timeout")
            break loop
        case <-ch1:
            fmt.Println("ch1")
            ch2 <- struct{}{}
        case <-ch2:
            fmt.Println("ch2")
        }
    }

}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading