Concurrency in 'Tour of Go' -


i'm going through 'a tour of go' , have been editing of lessons make sure understand them. have question regarding: https://tour.golang.org/concurrency/1

package main  import (     "fmt"     "time" )  func say(s string) {     := 0; < 5; i++ {         time.sleep(100 * time.millisecond)         fmt.println(s)     } }  func main() {     go say("world")     say("hello") } 

leaving main way produces random ordering of hellos , worlds because threads executing in different orders each time program runs. have 2 questions:

  1. if remove go line world , add line hello, world printed 5 times , hello not printed @ all. why that?
  2. if add go in front of both lines, nothing printed @ all. why that?

i have experience concurrency c++ (although while ago) , more recent experience python, describe overall experience concurrency novice-level.

thanks!

the program terminates before chance see results.

you can fix adding statement ensures main doesn't exit before other routines finished.


from golang - concurrency:

with goroutine return next line , don't wait function complete.

they give code example:

package main  import "fmt"  func f(n int) {   := 0; < 10; i++ {     fmt.println(n, ":", i)   } }  func main() {   go f(0)   var input string   fmt.scanln(&input) } 

in regards code above:

this why call scanln function has been included; without program exit before being given opportunity print numbers.


Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -