Skip to main content

Posts

Showing posts from June, 2018

Concurrency in Go - Visualization

In this blog, let us discuss a program to visualize concurrency in Golang. Reference and source: Program 1: import " fmt " import " time " func main () { var Ball int Ball = 5 table := make ( chan int ) go player (table, 1 ) go player (table, 2 ) fmt. Println ( " mainstart: " , Ball) table <- Ball time. Sleep ( 1 * time. Second ) endres := <- table fmt. Println ( " mainend: " , endres) } func player ( table chan int , playernumber int ){ for { ball := <- table fmt. Println (playernumber, " : " ,ball) ball++ time. Sleep ( 100 * time. Millisecond ) table <- ball } } /* ---------------------------------------------------------------------------------------------------------- Explanation: Please try to visualize on your own before reading the explanation. We will name the two goroutines that are called as player1_goroutine