+
+
+
+
+package main
+
+import (
+ "fmt"
+)
+
+func f(n int) {
+ for i := 1; i < 10; i++ {
+ fmt.Println(n, ":", i)
+ }
+}
+
+func main() {
+
+ go f(0)
+ var input string
+ fmt.Scanln(&input)
+
+}
+
+
+package main
+
+import "fmt"
+
+func f(n int) {
+ for i := 1; i < 10; i++ {
+ fmt.Println(n, ":", i)
+ }
+}
+
+func main() {
+ for i := 0; i < 10; i++ {
+ go f(i)
+ }
+
+ var input string
+ fmt.Scanln(&input)
+}
+
+
+package main
+
+import (
+ "fmt"
+ "math/rand"
+ "time"
+)
+
+func f(n int) {
+ for i := 1; i < 10; i++ {
+
+ duracao := time.Duration(rand.Intn(250))
+ time.Sleep(duracao * time.Millisecond)
+ fmt.Println(n, ":", i)
+ }
+}
+
+func main() {
+
+ for i := 0; i < 10; i++ {
+ go f(i)
+ }
+
+ var input string
+ fmt.Scanln(&input)
+}
+
+
+package main
+
+import (
+ "fmt"
+ "time"
+)
+
+func pinger(c chan string) {
+ for i := 0; ; i++ {
+ c <- fmt.Sprintf("%d: ping", i)
+ }
+}
+
+func printer(c chan string) {
+ for {
+ // msg :=
+ fmt.Println(<-c)
+ time.Sleep(time.Second * 1)
+ }
+}
+
+func main() {
+
+ var c chan string = make(chan string)
+
+ go pinger(c)
+ go printer(c)
+
+ var input string
+ fmt.Scanln(&input)
+
+}
+
+
+package main
+
+import (
+ "fmt"
+ "time"
+)
+
+func pinger(c chan string) {
+ for i := 1; ; i++ {
+ c <- fmt.Sprintf("%d: pinger", i)
+ }
+}
+
+func ponger(c chan string) {
+ for i := 1; ; i++ {
+ c <- fmt.Sprintf("%d: ponger", i)
+ }
+}
+
+func printer(c chan string) {
+ for {
+ fmt.Println(<-c)
+ time.Sleep(time.Second * 1)
+ }
+}
+
+func main() {
+
+ c := make(chan string)
+
+ go pinger(c)
+ go ponger(c)
+ go printer(c)
+
+ var input string
+ fmt.Scanln(&input)
+
+}
+
+
+package main
+
+import (
+ "fmt"
+ "time"
+)
+
+func pinger(c chan<- string) {
+ for i := 1; ; i++ {
+ c <- fmt.Sprintf("%d pinger", i)
+ }
+}
+
+func ponger(c chan<- string) {
+ for i := 1; ; i++ {
+ c <- fmt.Sprintf("%d ponger", i)
+ }
+}
+
+func printer(c <-chan string) {
+ for {
+ fmt.Println(<-c)
+ time.Sleep(time.Second * 1)
+ }
+}
+
+func main() {
+
+ c := make(chan string)
+
+ go pinger(c)
+ go ponger(c)
+ go printer(c)
+
+ var input string
+ fmt.Scanln(&input)
+
+}
+
+
+package main
+
+import (
+ "fmt"
+ "time"
+)
+
+func main() {
+
+ c1 := make(chan string)
+ c2 := make(chan string)
+
+ go func() {
+ for {
+ c1 <- "from 1"
+ time.Sleep(time.Second * 2)
+ }
+ }()
+
+ go func() {
+ for {
+ c2 <- "from 2"
+ time.Sleep(time.Second * 3)
+ }
+ }()
+
+ go func() {
+
+ for {
+ select {
+ case msg1 := <-c1:
+ fmt.Println(msg1)
+ case msg2 := <-c2:
+ fmt.Println(msg2)
+ }
+ }
+ }()
+
+ var input string
+ fmt.Scanln(&input)
+
+}
+
+
+package main
+
+import (
+ "fmt"
+ "time"
+)
+
+func main() {
+
+ c1 := make(chan string)
+ c2 := make(chan string)
+
+ go func() {
+ for {
+ c1 <- "Mensagem 1"
+ time.Sleep(time.Second * 2)
+ }
+ }()
+
+ go func() {
+ for {
+ c2 <- "Mensagem 2"
+ time.Sleep(time.Second * 3)
+ }
+ }()
+
+ go func() {
+ for {
+ select {
+ case msg1 := <-c1:
+ fmt.Println(msg1)
+ case msg2 := <-c2:
+ fmt.Println(msg2)
+ case <-time.After(time.Second):
+ fmt.Println("Timeout")
+ }
+ }
+ }()
+
+ var input string
+ fmt.Scanln(&input)
+
+}
+
+
+package main
+
+import (
+ "fmt"
+ "time"
+)
+
+func main() {
+
+ c1 := make(chan string, 1)
+ c2 := make(chan string, 1)
+
+ go func() {
+ for {
+ c1 <- "Canal 1"
+ time.Sleep(time.Second * 2)
+ }
+ }()
+
+ go func() {
+ for {
+ c2 <- "Canal 2"
+ time.Sleep(time.Second * 3)
+ }
+ }()
+
+ go func() {
+
+ for {
+ select {
+ case msg1 := <-c1:
+ fmt.Println(msg1)
+ case msg2 := <-c2:
+ fmt.Println(msg2)
+ case <-time.After(time.Second):
+ fmt.Println("Timeout")
+ default:
+ fmt.Println("Nada a ser lido")
+ time.Sleep(time.Millisecond * 500)
+ }
+ }
+
+ }()
+
+ var input string
+ fmt.Scanln(&input)
+}
+
+
+package main
+
+import (
+ "fmt"
+ "time"
+)
+
+func main() {
+
+ c1 := make(chan string, 3)
+ c2 := make(chan string, 1)
+
+ go func() {
+ for i := 1; ; i++ {
+ fmt.Println("Adicionando dados...")
+ c1 <- fmt.Sprintf("Canal 1: %d", i)
+ }
+ }()
+
+ go func() {
+ for i := 1; ; i++ {
+ c2 <- fmt.Sprintf("Canal 2: %d", i)
+ time.Sleep(time.Second * 2)
+ }
+ }()
+
+ go func() {
+ for {
+ select {
+ case msg1 := <-c1:
+ fmt.Println(msg1)
+ case msg2 := <-c2:
+ fmt.Println(msg2)
+ }
+ }
+ }()
+
+ var input string
+ fmt.Scanln(&input)
+}
+
+
+
+
+
+