vkorbes / aprendago

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

Exercício: Capítulo 9, Exercício 1 (Nível: 4) #27

Open vkorbes opened 3 years ago

vkorbes commented 3 years ago

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

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!

diegoparra commented 3 years ago

Fiquei um pouco na dúvida quanto a demonstrar os valores do array se isso deveria incluir mostrar os indexes tbm, então decidi só mostrar os valores atribuídos e não mostrar seus respectivos indexes.

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

haystem commented 3 years ago

Fiquei confuso na parte de printar pq usei Printf em tudo. Saiu o resultado mas ficou ruim para ler, não entendia pq n ia com printf. Depois que voltei a explicação entendi que na parte que ele lê o valor ele ja traz em numero e não precisa do printf % para direcionar.

package main

import("fmt")

func main(){
  array := [5]int{1,2,3,4,5}

  for ind,valor := range array{
    fmt.Println(ind,valor)
  }
    fmt.Printf("%T",array)
}
an4kein commented 3 years ago

Demorei um pouco em utilizar o range de maneira correta, mas após  forçar  um pouco o cérebro e mais algumas pesquisas no Spec e Effective, muita tentativa e erro, cheguei no resultado pedido no exercício:

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

package main

import (
    "fmt"
)

var array = [5]int{}

func main() {
    fmt.Println(`Usando uma literal composta:`)
    fmt.Println("")

    fmt.Println(`- Crie um array que suporte 5 valores to tipo int: `)
    fmt.Println(array)
    array[0] = 1
    array[1] = 2
    array[2] = 1337
    array[3] = 345
    array[4] = 45353

    fmt.Println("")
    fmt.Println(`- Atribua valores aos seus índices`)

    for i, v := range array {
        fmt.Printf("Indice: %v => Valor: %v\n", i, v)
    }
    fmt.Println("")
    fmt.Println(`- Utilize range e demonstre os valores do array.`)

    for i := range array {
        fmt.Println(array[i])
    }

    fmt.Println("")
    fmt.Println(`- Utilizando format printing, demonstre o tipo do array.`)

    fmt.Printf("Type array: %T", array)

}

Output

Usando uma literal composta:

- Crie um array que suporte 5 valores to tipo int: 
[0 0 0 0 0]

- Atribua valores aos seus índices
Indice: 0 => Valor: 1
Indice: 1 => Valor: 2
Indice: 2 => Valor: 1337
Indice: 3 => Valor: 345
Indice: 4 => Valor: 45353

- Utilize range e demonstre os valores do array.
1
2
1337
345
45353

- Utilizando format printing, demonstre o tipo do array.
Type array: [5]int
Program exited.
alansantosmg commented 3 years ago

package main

import "fmt"

func main() {

    myArray := [5]int{0, 1, 2, 3, 4}

    for _, v := range myArray {
        fmt.Println(v)
    }
    fmt.Printf("%T ", myArray)

}
Lucasmirandar commented 3 years ago

https://play.golang.org/p/BuKA70b-mAf

JPauloMoura commented 2 years ago

Crie um array que suporte 5 valores to tipo int, atribua valores aos seus índices, utilize range e demonstre os valores do array, Utilizando format printing demonstre o tipo do array..

package main

import "fmt"

func main() {
    arr := [4]int{10, 20, 30, 40}

    for i, v := range arr {
        fmt.Printf("Indíce %d: %d\n", i, v)
    }
    fmt.Printf("Tipo do array: %T", arr)
}

Resultado:

Indíce 0: 10
Indíce 1: 20
Indíce 2: 30
Indíce 3: 40
Tipo do array: [4]int%  

Resolução do Exercício


tomashugo commented 2 years ago
package main

import (
    "fmt"
)

func main() {
    var array [5]int

    array[0] = 10
    array[1] = 20
    array[2] = 30
    array[3] = 40
    array[4] = 50

    for _, value := range array {
        fmt.Println(value)
    }

    fmt.Printf("%T\n",array)
}
CaueFarias commented 2 years ago

package main

import ( "fmt" )

func main() {

array := [6]int{0, 1, 2, 3, 4, 5}

for índice, número := range array {

    fmt.Println("No índice", índice, "Temos o número: ", número)
}
fmt.Printf("%T", array)

}

viniciussanchez commented 2 years ago

https://go.dev/play/p/ahEiq9qxz4V

AlissonAp commented 2 years ago

https://go.dev/play/p/RsPzzrMM3Rs

wfrsilva commented 2 years ago

Cap. 9 – Exercícios: Nível #4 – 1 https://go.dev/play/p/SPZ9HDa5YGj

image

M3L1M commented 1 year ago

func main() { array := [5]int{1, 2, 3, 4, 5}

for indice, valor := range array {
    fmt.Println(indice, " - ", valor)
}

fmt.Printf("%T", array)

}

adelsonsljunior commented 1 year ago
package main

import (
    "fmt"
)

func main() {

    array := [5]int{1, 2, 3, 4, 5}
    for indice, valor := range array {
        fmt.Println(indice, valor)
    }
    fmt.Printf("%T", array)
}
DominMFD commented 2 months ago

https://go.dev/play/p/agOTvbRe4Wa

thiagoCalazans-dev commented 2 months ago

https://go.dev/play/p/gjbZogaeCeM

Vitor-Zen commented 3 weeks ago

https://go.dev/play/p/8IS4kH2cSN2