Added ping-pong exercise to practice goroutines
Currently trying to figure out concurrency without suffering a headache, stay tuned for more black magic using channels
This commit is contained in:
parent
8b87cce11c
commit
1279df8aab
|
@ -0,0 +1,3 @@
|
|||
module ping-pong
|
||||
|
||||
go 1.22.1
|
|
@ -0,0 +1,107 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
|
||||
var ball int = 0
|
||||
|
||||
// Credits to this tutorial for actually teaching how to work with channels
|
||||
// https://riptutorial.com/go/example/6056/ping-pong-with-two-goroutines
|
||||
|
||||
// Okay so from what I'm gathering here, the function input for the channel takes in an arrow depending on whether
|
||||
// or not it's getting a value from the channel, or sending a value into the channel, here it's pulling from
|
||||
// pingChan since the arrow is at the left
|
||||
|
||||
// For the second input value it's gonna be pulling the value from the pong Channel instead of sending to it
|
||||
func ping(pingChan <-chan int, pongChan chan<- int) {
|
||||
for {
|
||||
<-pingChan
|
||||
fmt.Println("PING:", ball)
|
||||
fmt.Printf("Current goroutine: %v\n\n", pingChan)
|
||||
ball = ball + 1
|
||||
time.Sleep(time.Second)
|
||||
// Instead of calling each other's function, we seem to initialize each goroutine by directing a value
|
||||
// into its channel
|
||||
pongChan <- 1
|
||||
}
|
||||
}
|
||||
|
||||
// Over here it should be opposite, since the ping func sends its value into pingChan, then here we need to
|
||||
// pull the value from pingChan, and in the case of pongChan it's the opposite
|
||||
func pong(pingChan chan<- int, pongChan <-chan int) {
|
||||
for {
|
||||
<-pongChan
|
||||
fmt.Println("PONG:", ball)
|
||||
fmt.Printf("Current goroutine: %v\n\n", pongChan)
|
||||
ball = ball + 1
|
||||
time.Sleep(time.Second)
|
||||
pingChan <- 1
|
||||
}
|
||||
}
|
||||
|
||||
// This is some black magic
|
||||
func main() {
|
||||
|
||||
// Here we initialize both channels, and provide both to each function called through "go"
|
||||
pingChannel := make(chan int)
|
||||
pongChannel := make(chan int)
|
||||
go ping(pingChannel, pongChannel)
|
||||
go pong(pingChannel, pongChannel)
|
||||
|
||||
// Now we're just initalizing them with a 1
|
||||
pingChannel <- 1
|
||||
|
||||
for {
|
||||
time.Sleep(time.Second)
|
||||
if ball >= 10 {
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
fmt.Println("Finished")
|
||||
|
||||
}
|
||||
|
||||
// WARNING: Do not read the following if you don't wish to have your retinas burned!
|
||||
|
||||
// var pingChannel chan int
|
||||
// var ball int
|
||||
//
|
||||
// func main() {
|
||||
// pingChannel := make(chan int)
|
||||
// fmt.Println("Hello world")
|
||||
// ball := 0
|
||||
// go ping(pingChannel)
|
||||
// //go pong(ball, pingChannel)
|
||||
// ping <- 1
|
||||
//
|
||||
// for {
|
||||
// if ball >= 10 {
|
||||
// fmt.Println("Ball value above 10")
|
||||
// os.Exit(0)
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// func ping(ballChannel <-chan int, ponger chan<- int) {
|
||||
// pingChannel := make(chan int)
|
||||
//
|
||||
// ball = <-ballChannel
|
||||
// fmt.Println("PING", ball)
|
||||
// ball = +1
|
||||
//
|
||||
// ballChannel <- ball
|
||||
// go pong(pingChannel)
|
||||
// }
|
||||
//
|
||||
// func pong(ballChannel chan int, ponger chan<- int) {
|
||||
// pingChannel := make(chan int)
|
||||
//
|
||||
// ball = <-ballChannel
|
||||
// fmt.Println("PONG", ball)
|
||||
//
|
||||
// ballChannel <- ball
|
||||
// go ping(pingChannel)
|
||||
// }
|
Loading…
Reference in New Issue