Featured image of post Go Async Channel

Go Async Channel

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
func asyncNotify(f func()) <-chan struct{} {
	ch := make(chan struct{})
	go func() {
		f()
		close(ch)
	}()
	return ch
}

func asyncMessage[T any](ch chan T, f func() (T, bool)) <-chan T {
	go func() {
		for {
			v, b := f()
			if !b {
				break
			}
			ch <- v
		}
		close(ch)
	}()
	return ch
}
Licensed under CC BY-NC-SA 4.0