vkorbes / aprendago

Curso completo em português da linguagem Go, de zero a ninja! 🇧🇷
http://aprendago.com
991 stars 180 forks source link

Exercício: Capítulo 20, Exercício 1 (Nível: 9) #59

Open vkorbes opened 3 years ago

vkorbes commented 3 years ago

Exercício: Capítulo 20, Exercício 1 (Nível: 9)

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 commented 3 years ago

https://play.golang.org/p/3biKewLYs_5

Tinha feito quase tudo certo, so esqueci de por o principal que seria o go func goroutine

package main

import (
    "fmt"
    "runtime"
    "sync"
    "time"
)

/* - Alem da goroutine principal, crie duas outras goroutines.
- Cada goroutine adicional devem fazer um print separado.
- Utilize waitgroups para fazer com que suas goroutines finalizem antes de o programa terminar. */

// https://github.com/golang/go/commit/1eebb91a5828c26532125b9464c92f721cd79d0f
// https://stackoverflow.com/questions/20728767/all-possible-goos-value
var goosList = []string{"android", "darwin", "dragonfly", "freebsd",
    "linux", "nacl", "netbsd", "openbsd",
    "plan9", "solaris", "windows"}

var wg1 sync.WaitGroup

func ImThink() {
    switch os := runtime.GOOS; os {
    case "android":
        fmt.Println("OS:", os)
    case "darwin":
        fmt.Println("OS:", os)
    case "linux":
        fmt.Println("OS:", os)
    case "windows":
        fmt.Println("OS:", os)

    }
    wg1.Done()
}

func VamosContar() {
    for i := 1; i < 10; i++ {
        //time.Sleep(1 * time.Second)
        time.Sleep(20)
        fmt.Printf("%v\t", i)
    }
    fmt.Println("")
    wg1.Done()
}

func ListItems() {
    for i := range goosList {
        //time.Sleep(1 * time.Second)
        //time.Sleep(20)
        fmt.Printf("%v\t", goosList[i])
    }
    fmt.Println("")
    wg1.Done()
}

func main() {
    fmt.Println("NumCPU Start: ", runtime.NumCPU())
    fmt.Println("NumGoroutine Start:", runtime.NumGoroutine())
    wg1.Add(3)
    go ImThink()
    go VamosContar()
    go ListItems()
    fmt.Println("NumGoroutine end: ", runtime.NumGoroutine())
    wg1.Wait()
}

Output

NumCPU Start:  8
NumGoroutine Start: 1
NumGoroutine end:  4
OS: linux
android darwin  dragonfly   freebsd linux   nacl    netbsd  openbsd plan9   solaris windows 
1   2   3   4   5   6   7   8   9   

Program exited.

image

alansantosmg commented 3 years ago

package main

import (
    "fmt"
    "sync"
)

var waitGroup sync.WaitGroup

func main() {

    waitGroup.Add(3)

    go func() {
        fmt.Println("Goroutine 1")
        waitGroup.Done()
    }()

    func() {
        fmt.Println("Goroutine 2")
        waitGroup.Done()
    }()

    func() {
        fmt.Println("Goroutine 3")
        waitGroup.Done()
    }()

    waitGroup.Wait()

}
LelecoNN commented 8 months ago

Playground

package main

import (
"fmt"
"sync"
)

var nr sync.WaitGroup
var counte int

func main() {
fmt.Println("grupo Principal")
nr.Add(2)
go soberano(2023, 25)
go NumPar()
nr.Wait()
}
func NumPar() {
for i := 2; i < 20; i++ {
counte = 0
func() {
if i%2 == 0 {

            counte = i
        }

    }()
    if counte != 0 {
        fmt.Println("Numero", counte)
    }

}
nr.Done()
}

func soberano(anoAtual, idade int) {
ano := anoAtual - idade
fmt.Println("Ano do Nacimento é ", ano)
nr.Done()
}