-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #53 from fabianoflorentino/development
Development to Main
- Loading branch information
Showing
9 changed files
with
329 additions
and
2 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
--- | ||
description: | ||
name: "Capítulo 22: Exercícios Ninja Nível 10" | ||
sections: | ||
- title: "Na prática: Exercício #1" | ||
text: | | ||
- Nível 10?! Êita! Parabéns! | ||
- Faça esse código funcionar: https://play.golang.org/p/j-EA6003P0 | ||
- Usando uma função anônima auto-executável | ||
- Usando buffer | ||
- Solução: | ||
- 1. https://play.golang.org/p/MNqpJ29FZJ | ||
- 2. https://play.golang.org/p/Y0Hx6IZc3U | ||
- title: "Na prática: Exercício #2" | ||
text: | | ||
- Faça esse código funcionar: https://play.golang.org/p/oB-p3KMiH6 | ||
- Solução: https://play.golang.org/p/isnJ8hMMKg | ||
- title: "Na prática: Exercício #3" | ||
text: | | ||
- Utilizando este código: https://play.golang.org/p/sfyu4Is3FG | ||
- ...use um for range loop para demonstrar os valores do canal. | ||
- Solução: https://play.golang.org/p/N2N6oN3f0b | ||
- title: "Na prática: Exercício #4" | ||
text: | | ||
- Utilizando este código: https://play.golang.org/p/MvL6uamrJP | ||
- ...use um select statement para demonstrar os valores do canal. | ||
- Solução: https://play.golang.org/p/UeJweL3Ola | ||
- title: "Na prática: Exercício #5" | ||
text: | | ||
- Utilizando este código: https://play.golang.org/p/YHOMV9NYKK | ||
- ...demonstre o comma ok idiom. | ||
- Solução: https://play.golang.org/p/qh2ywLB5OG | ||
- title: "Na prática: Exercício #6" | ||
text: | | ||
- Escreva um programa que coloque 100 números em um canal, retire os números do canal, e demonstre-os. | ||
- Solução: https://github.com/ellenkorbes/aprendago/blob/master/c%C3%B3digo/22_exercicios-ninja-10/06/main.go | ||
- title: "Na prática: Exercício #7" | ||
text: | | ||
- Crie um programa que lance 10 goroutines onde cada uma envia 10 números a um canal; | ||
- Tire estes números do canal e demonstre-os. | ||
- Solução: https://github.com/ellenkorbes/aprendago/blob/master/c%C3%B3digo/22_exercicios-ninja-10/07/main.go |
169 changes: 169 additions & 0 deletions
169
internal/exercicios_ninja_nivel_10/resolution_exercises.go
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,169 @@ | ||
package exercicios_ninja_nivel_10 | ||
|
||
import "fmt" | ||
|
||
// ResolucaoNaPraticaExercicio1 demonstrates a simple use of a goroutine and a channel in Go. | ||
// It creates an unbuffered channel of type int and starts a goroutine that sends the value 42 into the channel. | ||
// The main function then receives the value from the channel and prints it using fmt.Printf. | ||
func ResolucaoNaPraticaExercicio1() { | ||
channel := make(chan int) | ||
go func() { | ||
channel <- 42 | ||
}() | ||
|
||
fmt.Printf("Value: %v\n", <-channel) | ||
} | ||
|
||
// ResolucaoNaPraticaExercicio2 demonstrates the use of unidirectional channels in Go. | ||
// It creates a bidirectional channel and converts it to a send-only channel. | ||
// A goroutine is used to send a value (42) to the channel. | ||
// The main function then receives the value from the channel and prints it. | ||
// Finally, it prints the type of the send-only channel. | ||
func ResolucaoNaPraticaExercicio2() { | ||
channel := make(chan int) | ||
channel_send := chan<- int(channel) | ||
|
||
go func() { | ||
channel_send <- 42 | ||
}() | ||
|
||
fmt.Printf("Value: %v\n", <-channel) | ||
fmt.Printf("--------------------\n") | ||
fmt.Printf("Type: %T\n", channel_send) | ||
} | ||
|
||
// ResolucaoNaPraticaExercicio3 demonstrates the process of generating a channel, | ||
// receiving values from it, and then printing a message before exiting. | ||
// It first calls generateChannels() to create a channel, then passes this channel | ||
// to receiveChannel() to handle the received values. Finally, it prints a message | ||
// indicating that the program is about to exit. | ||
func ResolucaoNaPraticaExercicio3() { | ||
channel := generateExercicio3() | ||
receiveExercicio3(channel) | ||
|
||
fmt.Printf("About to exit...\n") | ||
} | ||
|
||
func ResolucaoNaPraticaExercicio4() { | ||
slct := make(chan int) | ||
channel := generateExercicio4(slct) | ||
|
||
go receiveExercicio4(channel, slct) | ||
|
||
fmt.Println("About to exit...") | ||
} | ||
|
||
func ResolucaoNaPraticaExercicio5() { | ||
channel := make(chan int) | ||
|
||
go func() { channel <- 42 }() | ||
|
||
value, ok := <-channel | ||
fmt.Printf("Value: %v\n", value) | ||
fmt.Printf("Channel open: %v\n", ok) | ||
close(channel) | ||
|
||
value, ok = <-channel | ||
fmt.Printf("Value: %v\n", value) | ||
fmt.Printf("Channel open: %v\n", ok) | ||
} | ||
|
||
// ResolucaoNaPraticaExercicio6 demonstrates the use of goroutines and channels in Go. | ||
// It creates a channel to communicate between a goroutine and the main function. | ||
// The goroutine sends integers from 0 to 100 to the channel and then closes the channel. | ||
// The main function receives and prints these integers from the channel until it is closed. | ||
func ResolucaoNaPraticaExercicio6() { | ||
channel := make(chan int) | ||
|
||
go func() { | ||
for idx := 0; idx < 101; idx++ { | ||
channel <- idx | ||
} | ||
close(channel) | ||
}() | ||
|
||
for value := range channel { | ||
fmt.Printf("Channel: %v\n", value) | ||
} | ||
} | ||
|
||
// ResolucaoNaPraticaExercicio7 demonstrates the use of goroutines and channels in Go. | ||
// It creates a channel to communicate between goroutines and the main function. | ||
// The function starts 10 goroutines, each sending the numbers 0 to 9 to the channel. | ||
// The main function then receives and prints 100 numbers from the channel. | ||
func ResolucaoNaPraticaExercicio7() { | ||
channel := make(chan int) | ||
|
||
for idx := 0; idx < 10; idx++ { | ||
go func() { | ||
for number := 0; number < 10; number++ { | ||
channel <- number | ||
} | ||
}() | ||
} | ||
|
||
for number := 0; number < 100; number++ { | ||
fmt.Println(number, "\t", <-channel) | ||
} | ||
} | ||
|
||
// generateChannels creates and returns a read-only channel of integers. | ||
// It launches 100 goroutines, each sending its index value to the channel. | ||
// The channel is not closed, so the caller must handle the channel closure if needed. | ||
func generateExercicio3() <-chan int { | ||
channel := make(chan int) | ||
|
||
go func() { | ||
for idx := 0; idx < 100; idx++ { | ||
channel <- idx | ||
} | ||
close(channel) | ||
}() | ||
|
||
return channel | ||
} | ||
|
||
// receiveChannel reads values from a read-only integer channel and prints each value to the standard output. | ||
// It continuously listens to the channel until it is closed, printing each received value with a formatted message. | ||
func receiveExercicio3(channel <-chan int) { | ||
for value := range channel { | ||
fmt.Printf("Value: %v\n", value) | ||
} | ||
} | ||
|
||
// generateExercicio4 creates and returns a read-only channel that emits integers from 0 to 99. | ||
// It also takes a send-only channel as an argument, which it uses to signal completion by sending a value of 0. | ||
// The function launches a goroutine that iterates from 0 to 99, sending each value to the returned channel. | ||
// After sending all values, the channel is closed and a signal is sent to the provided channel to indicate completion. | ||
func generateExercicio4(slct chan<- int) <-chan int { | ||
channel := make(chan int) | ||
|
||
go func() { | ||
for idx := 0; idx < 100; idx++ { | ||
channel <- idx | ||
} | ||
close(channel) | ||
slct <- 0 | ||
}() | ||
|
||
return channel | ||
} | ||
|
||
// receiveExercicio4 listens to two channels and prints the received values. | ||
// It runs an infinite loop where it uses a select statement to wait for values | ||
// from either the 'channel' or 'slct' channels. When a value is received from | ||
// either channel, it prints the value to the standard output. | ||
// | ||
// Parameters: | ||
// - channel: a read-only channel of integers from which values are received. | ||
// - slct: a read-only channel of integers from which values are received. | ||
func receiveExercicio4(channel <-chan int, slct chan int) { | ||
for { | ||
select { | ||
case value := <-channel: | ||
fmt.Printf("Value: %v\n", value) | ||
case value := <-slct: | ||
fmt.Printf("Value: %v\n", value) | ||
} | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package exercicios_ninja_nivel_10 | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/fabianoflorentino/aprendago/pkg/format" | ||
) | ||
|
||
const ( | ||
rootDir = "internal/exercicios_ninja_nivel_10" | ||
) | ||
|
||
func ExerciciosNinjaNivel10() { | ||
fmt.Printf("\n\nCapítulo 21: Exercícios Ninja Nível 10\n") | ||
|
||
executeSection("Na prática: Exercício #1") | ||
executeSection("Na prática: Exercício #2") | ||
executeSection("Na prática: Exercício #3") | ||
executeSection("Na prática: Exercício #4") | ||
executeSection("Na prática: Exercício #5") | ||
executeSection("Na prática: Exercício #6") | ||
executeSection("Na prática: Exercício #7") | ||
} | ||
|
||
func MenuExerciciosNinjaNivel10([]string) []format.MenuOptions { | ||
return []format.MenuOptions{ | ||
{Options: "--na-pratica-exercicio-1 --nivel-10", ExecFunc: func() { executeSection("Na prática: Exercício #1") }}, | ||
{Options: "--na-pratica-exercicio-1 --nivel-10 --resolucao", ExecFunc: func() { ResolucaoNaPraticaExercicio1() }}, | ||
{Options: "--na-pratica-exercicio-2 --nivel-10", ExecFunc: func() { executeSection("Na prática: Exercício #2") }}, | ||
{Options: "--na-pratica-exercicio-2 --nivel-10 --resolucao", ExecFunc: func() { ResolucaoNaPraticaExercicio2() }}, | ||
{Options: "--na-pratica-exercicio-3 --nivel-10", ExecFunc: func() { executeSection("Na prática: Exercício #3") }}, | ||
{Options: "--na-pratica-exercicio-3 --nivel-10 --resolucao", ExecFunc: func() { ResolucaoNaPraticaExercicio3() }}, | ||
{Options: "--na-pratica-exercicio-4 --nivel-10", ExecFunc: func() { executeSection("Na prática: Exercício #4") }}, | ||
{Options: "--na-pratica-exercicio-4 --nivel-10 --resolucao", ExecFunc: func() { ResolucaoNaPraticaExercicio4() }}, | ||
{Options: "--na-pratica-exercicio-5 --nivel-10", ExecFunc: func() { executeSection("Na prática: Exercício #5") }}, | ||
{Options: "--na-pratica-exercicio-5 --nivel-10 --resolucao", ExecFunc: func() { ResolucaoNaPraticaExercicio5() }}, | ||
{Options: "--na-pratica-exercicio-6 --nivel-10", ExecFunc: func() { executeSection("Na prática: Exercício #6") }}, | ||
{Options: "--na-pratica-exercicio-6 --nivel-10 --resolucao", ExecFunc: func() { ResolucaoNaPraticaExercicio6() }}, | ||
{Options: "--na-pratica-exercicio-7 --nivel-10", ExecFunc: func() { executeSection("Na prática: Exercício #7") }}, | ||
{Options: "--na-pratica-exercicio-7 --nivel-10 --resolucao", ExecFunc: func() { ResolucaoNaPraticaExercicio7() }}, | ||
} | ||
} | ||
|
||
func HelpMeExerciciosNinjaNivel10() { | ||
help := []format.HelpMe{ | ||
{Flag: "--na-pratica-exercicio-1 --nivel-10", Description: "Exibe a descrição do Exercício #1"}, | ||
{Flag: "--na-pratica-exercicio-1 --nivel-10 --resolucao", Description: "Exibe a resolução do Exercício #1"}, | ||
{Flag: "--na-pratica-exercicio-2 --nivel-10", Description: "Exibe a descrição do Exercício #2"}, | ||
{Flag: "--na-pratica-exercicio-2 --nivel-10 --resolucao", Description: "Exibe a resolução do Exercício #2"}, | ||
{Flag: "--na-pratica-exercicio-3 --nivel-10", Description: "Exibe a descrição do Exercício #3"}, | ||
{Flag: "--na-pratica-exercicio-3 --nivel-10 --resolucao", Description: "Exibe a resolução do Exercício #3"}, | ||
{Flag: "--na-pratica-exercicio-4 --nivel-10", Description: "Exibe a descrição do Exercício #4"}, | ||
{Flag: "--na-pratica-exercicio-4 --nivel-10 --resolucao", Description: "Exibe a resolução do Exercício #4"}, | ||
{Flag: "--na-pratica-exercicio-5 --nivel-10", Description: "Exibe a descrição do Exercício #5"}, | ||
{Flag: "--na-pratica-exercicio-5 --nivel-10 --resolucao", Description: "Exibe a resolução do Exercício #5"}, | ||
{Flag: "--na-pratica-exercicio-6 --nivel-10", Description: "Exibe a descrição do Exercício #6"}, | ||
{Flag: "--na-pratica-exercicio-6 --nivel-10 --resolucao", Description: "Exibe a resolução do Exercício #6"}, | ||
{Flag: "--na-pratica-exercicio-7 --nivel-10", Description: "Exibe a descrição do Exercício #7"}, | ||
{Flag: "--na-pratica-exercicio-7 --nivel-10 --resolucao", Description: "Exibe a resolução do Exercício #7"}, | ||
} | ||
|
||
fmt.Printf("\n\nCapítulo 21: Exercícios Ninja Nível 10\n") | ||
format.PrintHelpMe(help) | ||
} | ||
|
||
func executeSection(section string) { | ||
format.FormatSection(rootDir, section) | ||
} |
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
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
Oops, something went wrong.