Last active
February 9, 2024 10:02
-
-
Save bkataru/9c01ab588e8cccf68c5a566c52d3bb7d to your computer and use it in GitHub Desktop.
OwO d-demonstrating c-channel chans in go ( ͡° ͜ʖ ͡°)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| import ( | |
| "fmt" | |
| "time" | |
| ) | |
| func main () { | |
| var c = make(chan int, 5) | |
| go process(c) | |
| for i := range c { | |
| fmt.Printf("Main: %v\n", i) | |
| time.Sleep(time.Second * 1) | |
| } | |
| } | |
| func process(c chan int) { | |
| defer close(c) | |
| for i := 0; i < 5; i++ { | |
| fmt.Printf("Process: %v\n", i) | |
| c <- i | |
| fmt.Printf("Channel: %v\n", i) | |
| } | |
| fmt.Println("Exiting process...") | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| import "fmt" | |
| func main() { | |
| fmt.Println("OwO d-demonstrating c-channel chans in go ( ͡° ͜ʖ ͡°)") | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| import ( | |
| "fmt" | |
| "time" | |
| ) | |
| func main () { | |
| var c = make(chan int) | |
| go process(c) | |
| for i := range c { | |
| fmt.Printf("Main: %v\n", i) | |
| time.Sleep(time.Second * 1) // simulating doing some work | |
| } | |
| } | |
| func process(c chan int) { | |
| defer close(c) | |
| for i := 0; i < 5; i++ { | |
| fmt.Printf("Process: %v\n", i) | |
| c <- i | |
| fmt.Printf("Channel: %v\n", i) | |
| } | |
| fmt.Println("Exiting process...") | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| import ( | |
| "fmt" | |
| ) | |
| func main () { | |
| var c = make(chan int) | |
| go process(c) | |
| for i := range c { | |
| fmt.Printf("Main: %v\n", i) | |
| } | |
| } | |
| func process(c chan int) { | |
| defer close(c) | |
| for i := 0; i < 5; i++ { | |
| fmt.Printf("Process: %v\n", i) | |
| c <- i | |
| fmt.Printf("Channel: %v\n", i) | |
| } | |
| fmt.Println("Exiting process...") | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment