vkorbes / aprendago

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

Exercício: Capítulo 13, Exercício 3 (Nível: 6) #43

Open vkorbes opened 4 years ago

vkorbes commented 4 years ago

Exercício: Capítulo 13, Exercício 3 (Nível: 6)

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!

dsmello commented 4 years ago

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

diegoparra commented 3 years ago

fui no simples tbm: https://play.golang.org/p/LdHBH5qIZ5J

an4kein commented 3 years ago

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

package main

import "fmt"

/* - Utilize a declaração defer de maneira que demonstre que sua execução
só ocorre ao final do contexto ao qual ela pertence.
*/

func malig(x []int) int {
    soma := 0
    for _, i := range x {
        soma += i
    }
    return soma
}

func main() {
    defer fmt.Println("isso vem depois pq eh um defer")
    lista := []int{10, 10, 10, 10, 10}
    fmt.Println(malig(lista))
}

Output

50
isso vem depois pq eh um defer

Program exited.
alansantosmg commented 3 years ago

my solution:

package main

import "fmt"

func main() {

    a := 10
    b := 50

    x := a + b
    defer fmt.Print(x)
    fmt.Print("O resultado da soma de A + B é: ")

}
gomesmatheus commented 2 years ago
package main

import "fmt"

func main() {
    defer fmt.Println("Acabou a contagem, obrigado a todos os envolvidos")

    x := 10
    fmt.Println("Vamos contar até", x, "!")
    for i := 1; i <= 10; i++ {
        fmt.Println(i)
    }
}
wfrsilva commented 2 years ago

Cap. 13 – Exercícios: Nível #6 – 3 https://go.dev/play/p/p8XYuOiQeeU

image

thiagoCalazans-dev commented 4 months ago

package main

import "fmt"

func main() {

    slice := []int{2, 3, 5}
    defer fmt.Println("vem depois da soma")
    fmt.Println(somaDeVariadico(slice...))
}

func somaDeVariadico(variadico ...int) int {
    total := 0

    for _, v := range variadico {
        total += v
    }
    return total
}