Skip to content

Instantly share code, notes, and snippets.

@voratham
Last active March 5, 2023 18:17
Show Gist options
  • Save voratham/751dad2f11bba8edf24ca566fbf3131a to your computer and use it in GitHub Desktop.
Save voratham/751dad2f11bba8edf24ca566fbf3131a to your computer and use it in GitHub Desktop.
example-go-concunrrecy-generator-func
package main
import (
"fmt"
)
func main() {
ch := make(chan int)
ch2 := make(chan int)
go func() {
for i := 0; i < 5; i++ {
ch <- i
}
close(ch)
}()
go func() {
for i := 0; i < 5; i++ {
ch2 <- i
}
close(ch2)
}()
loop:
for {
select {
case val, ok := <-ch:
if ok {
fmt.Println("[ch] Received value:", val)
} else {
fmt.Println("[ch] Channel closed")
ch = nil
}
case val, ok := <-ch2:
if ok {
fmt.Println("[ch2] Received value:", val)
} else {
fmt.Println("[ch2] Channel closed")
ch2 = nil
}
default:
if ch == nil && ch2 == nil {
break loop
}
}
}
fmt.Println("πŸ”΄ end program...")
}
package main
import (
"fmt"
"sync"
)
func generator(count int) chan int {
ch := make(chan int)
go func() {
for i := 0; i < count; i++ {
ch <- i
}
close(ch)
}()
return ch
}
func main() {
pipelineForPub := make(chan int)
ch := generator(100)
ch2 := generator(100)
wg := sync.WaitGroup{}
go func() {
for data := range pipelineForPub {
fmt.Printf("πŸ”₯ data :: %+v\n ", data)
wg.Done()
}
}()
loop:
for {
select {
case val, ok := <-ch:
if ok {
// fmt.Println("[ch] Received value:", val)
wg.Add(1)
pipelineForPub <- val
} else {
fmt.Println("[ch] Channel closed")
ch = nil
}
case val, ok := <-ch2:
if ok {
// fmt.Println("[ch2] Received value:", val)
wg.Add(1)
pipelineForPub <- val
} else {
fmt.Println("[ch2] Channel closed")
ch2 = nil
}
default:
if ch == nil && ch2 == nil {
break loop
}
}
}
fmt.Println("πŸ”΄ wait program...")
wg.Wait()
fmt.Println("πŸ”΄ end program...")
}
package main
import (
"fmt"
"sync"
)
func main() {
ch := make(chan int)
ch2 := make(chan int)
pipelineForPub := make(chan int)
go func() {
for i := 0; i < 5; i++ {
ch <- i
}
close(ch)
}()
go func() {
for i := 0; i < 20; i++ {
ch2 <- i
}
close(ch2)
}()
wg := sync.WaitGroup{}
go func() {
for data := range pipelineForPub {
fmt.Printf("πŸ”₯ data :: %+v\n ", data)
wg.Done()
}
}()
loop:
for {
select {
case val, ok := <-ch:
if ok {
// fmt.Println("[ch] Received value:", val)
wg.Add(1)
pipelineForPub <- val
} else {
fmt.Println("[ch] Channel closed")
ch = nil
}
case val, ok := <-ch2:
if ok {
// fmt.Println("[ch2] Received value:", val)
wg.Add(1)
pipelineForPub <- val
} else {
fmt.Println("[ch2] Channel closed")
ch2 = nil
}
default:
if ch == nil && ch2 == nil {
break loop
}
}
}
fmt.Println("πŸ”΄ wait program...")
wg.Wait()
fmt.Println("πŸ”΄ end program...")
}
package main
import "fmt"
func readContent() chan int {
c := make(chan int)
go func() {
defer func() {
close(c) // close channel when read all data
}()
for i := 0; i < 1000; i++ {
if i == 10 {
fmt.Println("πŸŒ• skip index ", i)
continue
}
c <- i
}
}()
return c
}
func main() {
fmt.Println("🟒 start ")
receiveChannelContent := readContent()
count := 0
for {
select {
case data, ok := <-receiveChannelContent:
if !ok {
fmt.Println("πŸš€ close channel")
receiveChannelContent = nil
break
}
fmt.Printf("πŸ”₯ data :: %+v\n ", data)
count++
}
// ensure receiveChannelContent close channel break for {} loop
if receiveChannelContent == nil {
break
}
}
fmt.Println("βœ… summary receiveChannelContent :: ", count) // actual 999 because skip i = 10
fmt.Println("πŸŽ‰ end program")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment