diff --git a/book/404.html b/book/404.html index 0d9d2ec..d0ec0d3 100644 --- a/book/404.html +++ b/book/404.html @@ -84,7 +84,7 @@ diff --git a/book/etapa_01/cap01/index.html b/book/etapa_01/cap01/index.html index 2e03772..ddad243 100644 --- a/book/etapa_01/cap01/index.html +++ b/book/etapa_01/cap01/index.html @@ -83,7 +83,7 @@ diff --git a/book/etapa_01/cap02/index.html b/book/etapa_01/cap02/index.html index 03562c5..7a9bf4d 100644 --- a/book/etapa_01/cap02/index.html +++ b/book/etapa_01/cap02/index.html @@ -83,7 +83,7 @@ diff --git a/book/etapa_01/cap03/index.html b/book/etapa_01/cap03/index.html index 7eec1ae..c257480 100644 --- a/book/etapa_01/cap03/index.html +++ b/book/etapa_01/cap03/index.html @@ -83,7 +83,7 @@ diff --git a/book/etapa_01/cap04/index.html b/book/etapa_01/cap04/index.html index 0b1eeec..ce96c97 100644 --- a/book/etapa_01/cap04/index.html +++ b/book/etapa_01/cap04/index.html @@ -83,7 +83,7 @@ diff --git a/book/etapa_01/cap05/index.html b/book/etapa_01/cap05/index.html index 7d4fd4b..2aa1c7a 100644 --- a/book/etapa_01/cap05/index.html +++ b/book/etapa_01/cap05/index.html @@ -83,7 +83,7 @@ diff --git a/book/etapa_01/cap06/index.html b/book/etapa_01/cap06/index.html index 5d37ddf..b9d47a7 100644 --- a/book/etapa_01/cap06/index.html +++ b/book/etapa_01/cap06/index.html @@ -83,7 +83,7 @@ diff --git a/book/etapa_01/cap07/index.html b/book/etapa_01/cap07/index.html index a7b6426..1b61307 100644 --- a/book/etapa_01/cap07/index.html +++ b/book/etapa_01/cap07/index.html @@ -83,7 +83,7 @@ diff --git a/book/etapa_01/cap08/index.html b/book/etapa_01/cap08/index.html index 28e9882..f448813 100644 --- a/book/etapa_01/cap08/index.html +++ b/book/etapa_01/cap08/index.html @@ -83,7 +83,7 @@ diff --git a/book/etapa_01/cap09/index.html b/book/etapa_01/cap09/index.html index ad990cb..7a24a75 100644 --- a/book/etapa_01/cap09/index.html +++ b/book/etapa_01/cap09/index.html @@ -83,7 +83,7 @@ @@ -430,7 +430,7 @@

Exercício 02 - @@ -444,7 +444,7 @@

Exercício 02 - diff --git a/book/etapa_01/cap10/index.html b/book/etapa_01/cap10/index.html new file mode 100644 index 0000000..1c9819c --- /dev/null +++ b/book/etapa_01/cap10/index.html @@ -0,0 +1,601 @@ + + + + + + Capítulo 10 - Téo me to Go + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + + + + + + + + + + + + + + +
+ +
+ + + + + + + + +
+
+

Capítulo 10

+

Exemplos

+

Concorrência dentro de Main

+
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)
+
+}
+
+

Chamando de forma concorrente a mesma função várias vezes

+
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)
+}
+
+

Demonstração com time.Sleep()

+
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)
+}
+
+

Troca de informações entre Goroutines com canais

+
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)
+
+}
+
+

Mais de uma Goroutine enviando dados para o mesmo canal

+
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)
+
+}
+
+

Direção dos canais no momento de definição da função

+
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)
+
+}
+
+

Select para receber informações de canais que tem dados disponíveis

+
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)
+
+}
+
+

Timeout no Select quando não há dados disponíveis em nenhum canal

+
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)
+
+}
+
+

Valor default em Select

+
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)
+}
+
+

Buffer de canal

+
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)
+}
+
+ +
+ + +
+
+ + + +
+ + + + + + + + + + + + + + + + + + + + +
+ + diff --git a/book/etapa_01/index.html b/book/etapa_01/index.html index 72749c0..324b82c 100644 --- a/book/etapa_01/index.html +++ b/book/etapa_01/index.html @@ -83,7 +83,7 @@ diff --git a/book/etapa_02/index.html b/book/etapa_02/index.html index 05d1fba..9950914 100644 --- a/book/etapa_02/index.html +++ b/book/etapa_02/index.html @@ -83,7 +83,7 @@ @@ -173,7 +173,7 @@

Etapa 2