Open vkorbes opened 4 years ago
Fiz abaixo dessa forma. Utilizei duas formas diferentes do print para ajudar na fixação. Não senti dúvidas neste exercíco.
package main
import (
"fmt"
)
func main (){
x,y,z := 42, "James Bond",true
fmt.Println(x,y,z)
fmt.Printf("O valor de x: %v \n",x)
fmt.Printf("O valor de y: %v \n",y)
fmt.Printf("O valor de z: %v \n",z)
}
Senti dificuldade como foi mencionado, pra listar todos os valores em uma linha, mas acredito que com o fmt.Println atenda com o que foi pedido. Segue abaixo o código:
package main
import "fmt"
func main() {
x := 42
y := "James Bond"
z := true
fmt.Println(x, y, z)
fmt.Printf("%v \n", x)
fmt.Printf("%v \n", y)
fmt.Printf("%v \n", z)
}
Quando falou em operador curto, fiquei em dúvida se podia usar x, y, z := 42, "James bond", true
, mas segue minha solução:
package main
import (
"fmt"
)
func main() {
x := 42
y := "James Bond"
z := true
fmt.Println(x)
fmt.Println(y)
fmt.Println(z)
fmt.Println(x, y, z)
}
Minha solução tentando utilizar as funções Sprint() e Sprintf() e usando strings literals
package main
import "fmt"
func main() {
x := 42
y := "James Bond"
z := true
fmt.Println(x, y, z)
fmt.Println("==> Valor de x: ", x)
fmt.Println("==> Valor de x: ", y)
fmt.Println("==> Valor de x: ", z)
all := fmt.Sprint("valor de x: ", x, " valor de y: ", y, " valor de z: ", z)
fmt.Println("=> Valores formatos com Sprint: ", all)
lite := fmt.Sprintf(`=> Literals: Valor de x: %d | valor de y: %s | valor de z: %t`, x, y, z)
fmt.Println(lite)
}
Output:
42 James Bond true
==> Valor de x: 42
==> Valor de x: James Bond
==> Valor de x: true
=> Valores formatos com Sprint: valor de x: 42 valor de y: James Bond valor de z: true
=> Literals: Valor de x: 42 | valor de y: James Bond | valor de z: true
package main
import "fmt"
func main() {
x, y, z := 42, "James Bond", true
fmt.Printf("%v %v %v\n", x, y, z)
fmt.Println(x)
fmt.Println(y)
fmt.Println(z)
}
package main
import "fmt"
func main() {
// Declaração de valores com o operador curto de atribuição
x := 42 // int
y := "James Bond" // string
z := true // bool
// Imprime em uma única declaração
fmt.Println(x, y, z)
// Imprime em múltiplas declarações
fmt.Println(x)
fmt.Println(y)
fmt.Println(z)
}
Output:
42 James Bond true
42
James Bond
true
Não achei muito complicado so que nao sabia que tinha como fazer de duas maneiras diferente
Code:
package main
import (
"fmt"
)
func main() {
x := 42
y := "James Bond"
z := true
fmt.Println(x, y, z)
fmt.Println(x)
fmt.Println(y)
fmt.Println(z)
}
Output:
42 James Bond true
42
James Bond
true
Program exited.
https://play.golang.org/p/GQvqV0yWyLQ
Obs: curioso precisar de um package "fmt" para usar as funçoes print, em outras linguagens nao é necessario um pacote para chamar o print
https://play.golang.org/p/GQvqV0yWyLQ
Obs: curioso precisar de um package "fmt" para usar as funçoes print, em outras linguagens nao é necessario um pacote para chamar o print
Em C você precisa da lib stdio.h para usar printf.
Nativamente o Go tem o print, mas ele é usado para algo mais técnico e voltado para Debug, o pacote fmt trata vários comportamentos e como mencionado pela Ellen no Cap1 e 2 você consegue saber a quantidade de parametros passados, além de tratar retorno de erro. qtParams, err := fmt.Printf("%T", x)
package main
import "fmt"
func main() {
x := 42
y := "James Bond"
z := true
fmt.Printf("%d %s %v\n", x, y, z)
fmt.Printf("%v\n", x)
fmt.Printf("%v\n", y)
fmt.Printf("%v\n", z)
}
package main
import "fmt"
func main(){
x := 42
y := "James Bond"
z := true
fmt.Printf("%v %v %v\n", x, y, z)
fmt.Println(x)
fmt.Println(y)
fmt.Println(z)
}
package main
import "fmt"
func main() {
x := 42
y := "James Bond"
z := true
fmt.Println("\nVários prints juntos: ")
fmt.Println(fmt.Sprintf("\t%v %v %v", x, y, z)) // Coloquei o Sprintf dentro do Println pois não queria ficar adiocionando \n no final de cada Printf.
fmt.Println("\nPrints separados agora: ")
fmt.Println(fmt.Sprintf("\tValor de x: %v", x))
fmt.Println(fmt.Sprintf("\tValor de y: %v", y))
fmt.Println(fmt.Sprintf("\tValor de z: %v\n", z))
}
Output:
Vários prints juntos:
42 James Bond true
Prints separados agora:
Valor de x: 42
Valor de y: James Bond
Valor de z: true
import (
"fmt"
)
func main() {
x := 42
y := "James Bond"
z := true
fmt.Printf("%v, %v anos, espião ? %v\n\n",y, x, z)
fmt.Println("Idade :",x)
fmt.Println("Nome :",y)
fmt.Println("Espião :",z)
}
James Bond, 42 anos, espião ? true
Idade : 42
Nome : James Bond
Espião : true
https://play.golang.org/p/d1pq5QOJCkZ
[Dúvidas] Múltiplas declarações em uma única linha dificulta a leitura do código (na opinião de vocês)?
O meu ficou assim: https://play.golang.org/p/HmXH48bTjf-
https://go.dev/play/p/fIUtjAi0XY_a
Tive um impasse com a sintaxe do short operator, pq estava usando junto com o var var x := 42
, mas que solucionado a tempo ^^
package main
import "fmt"
func main() {
// Utilizando o operador curto de declaração, atribua valores às variáveis
x, y, z := 42, "James Bond", true
// Demonstre os valores das variáveis com uma única declaração print
fmt.Println(x, y, z)
// Demonstre os valores das variáveis com multiplas declarações print
fmt.Printf("%v\n", x)
fmt.Printf("%v\n", y)
fmt.Printf("%v\n", z)
}
package main
import "fmt"
func main() {
x := 42
y := "James Bond"
z := true
fmt.Printf("X: %v | Y: %v | Z: %v\n", x, y, z)
fmt.Printf("X: %v\n", x)
fmt.Printf("Y: %v\n", y)
fmt.Printf("Z: %v\n", z)
}
Code:
// Main Package
package main
// Import of the packages
import (
"fmt"
)
// Main function
func main() {
// Variable declaration
x := 42
y := "James Bond"
z := true
// Using only one print declaration
fmt.Printf("X: %v, Y: %v, Z: %v\n\n", x, y, z)
// Using multiple print declarations
fmt.Println("X:", x)
fmt.Println("Y:", y)
fmt.Println("Z:", z)
}
Output:
X: 42, Y: James Bond, Z: true
X: 42
Y: James Bond
Z: true
package main
import "fmt"
func main() {
// atribuindo os valores
x, y, z := 42, "James Bond", true
// Demosntrando os valores com uma unica declaração print
fmt.Printf("%v, %v, %v.\n\n", x, y, z)
// Demonstrando os valores com múltiplas declaração print
fmt.Println(x)
fmt.Println(y)
fmt.Println(z)
}
42, James Bond, true.
42
James Bond
true
https://go.dev/play/p/ydE6kgs3k7M
package main
import "fmt"
func main() { x := 42 y := "James Bond" z := true
fmt.Printf("1: %v, 2: %v, 3: %v\n", x, y, z)
fmt.Printf("1: %T, 2: %T, 3: %T\n", x, y, z)
fmt.Println("1: ", x)
fmt.Println("2: ", y)
fmt.Println("3: ", z)
}
func main() { x := 47 y := "James Bond" z := true
fmt.Println(x)
fmt.Println(y)
fmt.Println(z)
fmt.Println(x, y, z)
}
Bem parecido com o da maioria, apresento a minha solução:
package main
import "fmt"
func main() {
x := 42
y := "James Bond"
z := true
fmt.Println(x)
fmt.Println(y)
fmt.Printf("x = %v\ny = %v\nz = %v", x, y, z)
}
package main
import "fmt"
func main() { x := 42 y := "James Bond" z := true
fmt.Println(x, y, z)
fmt.Println(x)
fmt.Println(y)
fmt.Println(z)
}
https://go.dev/play/p/AYa3_9dxYEK
// You can edit this code!
// Click here and start typing.
package main
import "fmt"
func main() {
x := 42
y := "James Bond"
z := true
fmt.Printf("%d\n%s\n%t", x, y, z)
fmt.Printf("%d\n", x)
fmt.Printf("%s\n", y)
fmt.Printf("%t", z)
}
-----------------------------------------------------------------------------------------------------------------------------------------------
Output
42
James Bond
true42
James Bond
true
Program exited.
-----------------------------------------------------------------------------------------------------------------------------------------------
https://go.dev/play/p/IsECU4-4JWu
package main
import (
"fmt"
)
func main() {
x := 42
y := "James Bond"
z := true
fmt.Println(x, y, z)
fmt.Println(x)
fmt.Println(y)
fmt.Println(z)
}
Exercício: Capítulo 3, Exercício 1 (Nível: 1)
Use esta thread para compartilhar sua solução, discutir o exercício com os colegas e pedir ajuda caso tenha dificuldades!