I want to print when the barber is "cutting hair" this may change
later to something else, I'd like to reach to a point where I kind
print Barber X is cutting hair to Customer Y. The problem that I'd
like to approach now is how to write content to the screen from a
gorountine? In the code below, if I remove the go statement from the
function, I can get content to the screen, but if I make it a
gorountine to run in the back, it does not print anything.
Any ideas? I know is quite basic, but I scanned Effective go for ir
and couldn't find anything specific.
This is what I have so far.
1 package main
2
3 import "fmt"
4
5 func main(){
6 //chBarbers:= make(chan int)
7 chCustomers:= make(chan int,1)
8 chMutex:= make(chan int,1)
9 //var waiting int = 0
10 fmt.Printf("HASD\n");
11
12 //chCustomers <- 1
13 //chMutex <- 1
14
15 go barber(chCustomers, chMutex);
16 }
17
18
19 func barber(chCustomers chan int , chMutex chan int ) {
20 for ;; {
21 // <- chCustomers
22 // <- chMutex
23 fmt.Printf("Cutting hair");
24 }
25
26 }
Thanks,
Nico
A Go program exits when its main function returns. With the "go" at
line 15, you don't wait around long enough to let the barber goroutine
print, before the program exits.
You could add a time.Sleep(1 * time.Second) at line 16. Or, if you
want to wait forever, use:
select {}
I attached the current working code.
THanks,
Nico
http://play.golang.org/p/tJj7rHmnxD
for ch, ok := <-lobby; ok ; ch, ok = <-lobby {
What is the meaning of the for above?
I think it's just a wordier version of -for ch:= range lobby{