Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Capítulo 22, Exercício 6 (Nível: 10) #82

Open
vkorbes opened this issue Oct 9, 2020 · 4 comments
Open

Capítulo 22, Exercício 6 (Nível: 10) #82

vkorbes opened this issue Oct 9, 2020 · 4 comments

Comments

@vkorbes
Copy link
Owner

vkorbes commented Oct 9, 2020

Exercício: Capítulo 22, Exercício 6 (Nível: 10)

Link para o vídeo:

Use esta thread para compartilhar sua solução, discutir o exercício com os colegas e pedir ajuda caso tenha dificuldades!

@an4kein
Copy link

an4kein commented Mar 16, 2021

https://play.golang.org/p/LSXTY6Qu2Qb

package main

import "fmt"

/* - Escreva um programa que coloque 100 números em um canal, retire os números do canal, e demonstre-os. */

func main() {
	novoCanal := coloque()
	for i := range novoCanal {
		fmt.Println("Canal recebeu:", i)
	}
}

func coloque() chan int {
	canal := make(chan int)
	go func() {
		for i := 1; i <= 100; i++ {
			canal <- i
		}
		close(canal)
	}()
	return canal
}

Output

Canal recebeu: 1
Canal recebeu: 2
Canal recebeu: 3
Canal recebeu: 4
Canal recebeu: 5
[...]
Canal recebeu: 97
Canal recebeu: 98
Canal recebeu: 99
Canal recebeu: 100

Program exited.

@joelgarciajr84
Copy link

`package main

import "fmt"

func main() {
totalMessages := 100
comChannel := make(chan int)
go sender(comChannel, totalMessages)
reader(comChannel)
}

func reader(comChannel chan int) {
for msg := range comChannel {
fmt.Println("MESSAGE RECEIVED FROM CHANNEL: ", msg)
}
}

func sender(channel chan int, totalMsgs int) {
for i := 0; i < totalMsgs; i++ {
channel <- i
}
close(channel)
}`

@LelecoNN
Copy link

Playground

package main

import "fmt"

func main() {
	canal1 := make(chan int)
	canal2 := receber(canal1)
	imprime(canal2)
	//fmt.Println()
}
func receber(canal1 chan int) <-chan int {
	go func() {
		for i := 0; i < 100; i++ {
			canal1 <- i
		}
		close(canal1)
	}()
	return canal1

}
func imprime(s <-chan int) {
	for v := range s {
		fmt.Println(v)
	}

}

@LeandroCGMS
Copy link

package main

import (
	"fmt"
)

func main() {
	c := make(chan int)
	go func() {
		for i := 1; i <= 100; i++ {
			c <- i
		}
		close(c)
	}()
	for {
		select {
			case v, ok := <- c:
				if ok {
					fmt.Println(v)
				} else {
					fmt.Println("Não há mais valores no canal c, \nvalor default ->", v)
					return
				}
		}
	}
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants