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 3, Exercício 1 (Nível: 1) #4

Open vkorbes opened 4 years ago

vkorbes commented 4 years ago

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

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 4 years ago

https://play.golang.org/p/6T0sFkTTu_w

rodrimor commented 3 years ago

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

haystem commented 3 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)
  }
yurimorales commented 3 years ago

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

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

guifeliper commented 3 years ago

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)
}
Julian-ie commented 3 years ago

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

devpwn commented 3 years ago

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

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

thiagoalgo commented 3 years ago

https://play.golang.org/p/wi-n-xovtij

wesleydutrads commented 3 years ago

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

vnessaethi commented 3 years ago
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)
}
victorinno commented 3 years ago

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

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

Não achei muito complicado so que nao sabia que tinha como fazer de duas maneiras diferente

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

lemesdaniel commented 3 years ago

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

rogerpoliver commented 3 years ago

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

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

Lucasmirandar commented 3 years ago

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

avlambertucci commented 3 years ago

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

tonnytg commented 3 years ago

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)

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

}
andersoncleyson commented 3 years ago
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)
}
guilherme-de-marchi commented 3 years ago
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
KevenMarioN commented 2 years ago
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)

}

Saída

James Bond, 42 anos, espião ? true

Idade : 42
Nome : James Bond
Espião : true
CarlosSMA commented 2 years ago

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)?

ltbatis commented 2 years ago

O meu ficou assim: https://play.golang.org/p/HmXH48bTjf-

araujodg commented 2 years ago

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 ^^

Guilheeeerme commented 2 years ago

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

henricker commented 2 years ago

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

AlissonAp commented 2 years ago

https://go.dev/play/p/3xXq1Zcu5kM

viniciusbmello commented 2 years ago
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)
}
Mluz490 commented 2 years ago

https://go.dev/play/p/-FVYDlm26bj

gustavomfc commented 2 years ago
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)
}
GabriewF commented 2 years ago

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

Link to Go Playground

kayquecoelho commented 2 years ago

https://go.dev/play/p/-iAL8c1OeC8

gean634n commented 1 year ago

Exercicio:

Solução no Go Playground

code:

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)
}

output:

42, James Bond, true.

42
James Bond
true
fennder commented 1 year ago

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)

}

izanf commented 1 year ago

https://go.dev/play/p/5uPSowKDdjx

M3L1M commented 1 year ago

func main() { x := 47 y := "James Bond" z := true

fmt.Println(x)
fmt.Println(y)
fmt.Println(z)

fmt.Println(x, y, z)

}

elyosemite commented 1 year ago

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)
}
andreeysiilva commented 1 year ago

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)

}

katiasveloso commented 1 year ago

https://go.dev/play/p/2B0ah53gtJ9

edlanelima commented 1 year ago

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

an4kein commented 1 year ago

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.

-----------------------------------------------------------------------------------------------------------------------------------------------
adelsonsljunior commented 1 year ago

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)
}
uiltonlopes commented 1 year ago

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

dilandehonbra commented 9 months ago

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

leonardoTavaresM commented 7 months ago

https://go.dev/play/p/2B0ah53gtJ9

JrOkean commented 7 months ago

https://github.com/JrOkean/Aprendendo-Go/blob/7c0af0d66ce32c96df5d60836aa3b14860980b3c/exercicio-n%C3%ADvel%231/um.go