aprendago
aprendago copied to clipboard
Exercício: Capítulo 9, Exercício 2 (Nível: 4)
Exercício: Capítulo 9, Exercício 2 (Nível: 4)
Use esta thread para compartilhar sua solução, discutir o exercício com os colegas e pedir ajuda caso tenha dificuldades!
https://play.golang.org/p/Xmuf4-dSlDE
https://play.golang.org/p/u24rA4vhu4C
fiz com strings e printf
package main
import("fmt")
func main(){
array := [10]string{"Ana","Joana","Siraya","Carla","Vivian","Betina","Laritssa","Bianca","Muana","Juressica"}
for indice, valor:= range array{
fmt.Printf("%d, %s \n", indice, valor)
}
fmt.Printf("%T",array)
}
https://play.golang.org/p/_pKZB7SlqC7
package main
import (
"fmt"
)
func main() {
fmt.Println(`Usando uma literal composta:`)
fmt.Println("")
slice := []int{}
fmt.Printf("- Crie uma slice de tipo int:")
fmt.Println("")
fmt.Printf("%T", slice)
fmt.Println("")
fmt.Println("")
fmt.Printf("- Atribua 10 valores a ela: \n")
slice = []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
fmt.Println(slice)
fmt.Println("")
fmt.Printf("- Utilize range para demonstrar todos estes valores.\n")
for v := range slice {
fmt.Println(slice[v])
}
fmt.Println("")
fmt.Printf("- E utilize format printing para demonstrar seu tipo.\n")
fmt.Printf("Type of slice: %T", slice)
}
Output
Usando uma literal composta:
- Crie uma slice de tipo int:
[]int
- Atribua 10 valores a ela:
[1 2 3 4 5 6 7 8 9 10]
- Utilize range para demonstrar todos estes valores.
1
2
3
4
5
6
7
8
9
10
- E utilize format printing para demonstrar seu tipo.
Type of slice: []int
Program exited.
package main
import "fmt"
func main() {
umaSlice := []int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
for _, v := range umaSlice {
fmt.Printf("%v ", v)
}
fmt.Printf("%T", umaSlice)
}
https://play.golang.org/p/oW-soISfjuE
2. Crie uma Slice do tipo int, atribua 10 valores a ela, utilize range para demonstrar todos estes valores, e utilize format printing para demonstrar seu tipo.
package main
import "fmt"
func main() {
slice := []int{0, 10, 20, 30, 40, 50, 70, 80, 90, 100}
for i, v := range slice {
fmt.Printf("Indíce %d: %d\n", i, v)
}
fmt.Printf("Tipo do slice: %T", slice)
}
Resultado:
Indíce 0: 0
Indíce 1: 10
Indíce 2: 20
Indíce 3: 30
Indíce 4: 40
Indíce 5: 50
Indíce 6: 70
Indíce 7: 80
Indíce 8: 90
Indíce 9: 100
Tipo do slice: []int
package main
import (
"fmt"
)
func main() {
slice := [10]int{1,2,3,4,5,6,7,8,9,10}
for key, value := range slice {
fmt.Printf("Índice: %v, Valor: %v\n",key, value)
}
fmt.Printf("%T\n",slice)
}
`package main
import ( "fmt" )
func main() {
slice := []int{10, 20, 30, 40, 50, 60, 70, 80, 90, 100}
for índice, número := range slice {
fmt.Println("No índice: ", índice, "Temos o valor: ", número)
}
fmt.Printf("%T", slice)
} `
https://go.dev/play/p/v193psTiVEf
https://go.dev/play/p/oa3YJ50-7_7
Cap. 9 – Exercícios: Nível #4 – 2
https://go.dev/play/p/DWOFNp-oj1N
func main() { slice := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
for indice, valor := range slice {
fmt.Printf("%v - %v\n", indice, valor)
}
fmt.Printf("%T", slice)
}
package main
import (
"fmt"
)
func main() {
slice := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
for indice, valor := range slice {
fmt.Println(indice, valor)
}
fmt.Printf("%T", slice)
}
https://go.dev/play/p/ZinSUU8LHEu
https://go.dev/play/p/gjbZogaeCeM
https://go.dev/play/p/w88tZmGewVB