Created
August 1, 2013 22:20
-
-
Save roberto/6135869 to your computer and use it in GitHub Desktop.
This file contains 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" | |
) | |
type Client struct { | |
name string | |
} | |
func barber() chan <- Client { | |
clients := make(chan Client, 1) | |
go func() { | |
for client := range clients { | |
fmt.Printf("Cliente %q sendo cortado\n", client.name) | |
time.Sleep(1) | |
fmt.Printf("Cliente %q satisfeito\n", client.name) | |
} | |
}() | |
return clients | |
} | |
func chair(barber chan<-Client) chan <- Client { | |
clients := make(chan Client, 3) | |
go func() { | |
for client := range clients { | |
fmt.Printf("Cliente %q aguardando\n", client.name) | |
time.Sleep(1) | |
select { | |
case barber <- client: | |
} | |
} | |
}() | |
return clients | |
} | |
func main() { | |
clients := []Client{ | |
{name: "Bob"}, | |
{name: "Zeca"}, | |
{name: "Mary"}, | |
{name: "Oz"}, | |
} | |
barber := barber() | |
chair := chair(barber) | |
for _, client := range clients { | |
fmt.Printf("Cliente %q entra na barbearia\n", client.name) | |
select { | |
case barber <- client: | |
default: | |
select { | |
case chair <- client: | |
default: fmt.Printf("Cliente %q desiste\n", client.name) | |
} | |
} | |
} | |
time.Sleep(1e9) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment