Skip to content

Instantly share code, notes, and snippets.

@robsongomes
Created June 30, 2022 19:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save robsongomes/cd994f72171c74ecbefe980a01b1e8bd to your computer and use it in GitHub Desktop.
Save robsongomes/cd994f72171c74ecbefe980a01b1e8bd to your computer and use it in GitHub Desktop.
Código desenvolvido na aula 28 do curso Conhecendo a Golang
package main
import (
"fmt"
"math"
"math/rand"
"time"
)
type Resultado struct {
minerador int
valor float64
}
func main() {
mina := make(chan Resultado)
blockchain := make(chan Resultado)
//minerar
go minerar(mina)
//enviar o bitcoin pra blockchain
go enviarParaBlockchain(mina, blockchain)
//imprimir o valor recebido
receberBitcoin(blockchain)
}
func receberBitcoin(blockchain chan Resultado) {
for b := range blockchain {
fmt.Printf("%f foi recebido para o blockchain\n", b.valor)
}
}
// minerou(x) -> se x > 0.5 -> blockchain
func enviarParaBlockchain(mineracao, blockchain chan Resultado) {
for r := range mineracao {
if math.Abs(1.0-r.valor) < 0.5 {
fmt.Printf("O VALOR %f FOI ENVIADO PARA O BLOCKCHAIN\n", r.valor)
blockchain <- r
} else {
fmt.Println("Valor foi abaixo de 0.5 e foi descartado", r.valor)
}
}
close(blockchain) //terminei de enviar os bitcoins
}
func minerar(mina chan Resultado) {
for i := 1; i <= 4; i++ {
minerarBitcoin(i, mina)
}
close(mina) //fecha o canal
}
func minerarBitcoin(minerador int, c chan Resultado) {
time.Sleep(time.Duration(rand.Intn(3)) * time.Second)
bitcoin := rand.Float64()
fmt.Printf("%f bitcoins minerados pelo minerador %d\n", bitcoin, minerador)
c <- Resultado{minerador, bitcoin}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment