Giter Club home page Giter Club logo

Comments (49)

devpwn avatar devpwn commented on August 22, 2024 5

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

from aprendago.

haystem avatar haystem commented on August 22, 2024 1

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

from aprendago.

tonnytg avatar tonnytg commented on August 22, 2024 1

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)

from aprendago.

andreeysiilva avatar andreeysiilva commented on August 22, 2024 1

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)

}

from aprendago.

katiasveloso avatar katiasveloso commented on August 22, 2024 1

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

from aprendago.

diegoparra avatar diegoparra commented on August 22, 2024

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

from aprendago.

rodrimor avatar rodrimor commented on August 22, 2024

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

from aprendago.

yurimorales avatar yurimorales commented on August 22, 2024

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

from aprendago.

AngeloDamasio avatar AngeloDamasio commented on August 22, 2024

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

from aprendago.

guifeliper avatar guifeliper commented on August 22, 2024

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

from aprendago.

Julian-ie avatar Julian-ie commented on August 22, 2024

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

from aprendago.

viniciussanchez avatar viniciussanchez commented on August 22, 2024

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

from aprendago.

thiagoalgo avatar thiagoalgo commented on August 22, 2024

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

from aprendago.

wesleydutrads avatar wesleydutrads commented on August 22, 2024

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

from aprendago.

vnessaethi avatar vnessaethi commented on August 22, 2024
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)
}

from aprendago.

victorinno avatar victorinno commented on August 22, 2024

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

from aprendago.

ygorsimoes avatar ygorsimoes commented on August 22, 2024
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

from aprendago.

felipesvrosa avatar felipesvrosa commented on August 22, 2024

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

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

from aprendago.

lemesdaniel avatar lemesdaniel commented on August 22, 2024

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

from aprendago.

rogerpoliver avatar rogerpoliver commented on August 22, 2024

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.

from aprendago.

exageraldo avatar exageraldo commented on August 22, 2024

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

from aprendago.

Lucasmirandar avatar Lucasmirandar commented on August 22, 2024

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

from aprendago.

avlambertucci avatar avlambertucci commented on August 22, 2024

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

from aprendago.

absnows avatar absnows commented on August 22, 2024
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)

}

from aprendago.

andersoncleyson avatar andersoncleyson commented on August 22, 2024
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)
}

from aprendago.

guilherme-de-marchi avatar guilherme-de-marchi commented on August 22, 2024
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

from aprendago.

KevenMarioN avatar KevenMarioN commented on August 22, 2024
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

from aprendago.

CarlosSMA avatar CarlosSMA commented on August 22, 2024

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

from aprendago.

ltbatis avatar ltbatis commented on August 22, 2024

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

from aprendago.

araujodg avatar araujodg commented on August 22, 2024

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

from aprendago.

Guilheeeerme avatar Guilheeeerme commented on August 22, 2024

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

from aprendago.

henricker avatar henricker commented on August 22, 2024

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

from aprendago.

AlissonAp avatar AlissonAp commented on August 22, 2024

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

from aprendago.

viniciusbmello avatar viniciusbmello commented on August 22, 2024
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)
}

from aprendago.

Mluz490 avatar Mluz490 commented on August 22, 2024

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

from aprendago.

gustavomfc avatar gustavomfc commented on August 22, 2024
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)
}

from aprendago.

GabriewF avatar GabriewF commented on August 22, 2024

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

from aprendago.

kayquecoelho avatar kayquecoelho commented on August 22, 2024

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

from aprendago.

gean634n avatar gean634n commented on August 22, 2024

Exercicio:

  • Utilizando o operador curto de declaração, atribua estes valores às variáveis com os identificadores "x", "y", e "z".
    1 - 42
    2 - "James Bond"
    3 - true
  • Agora demonstre os valores nestas variáveis, com:
    1 - Uma única declaração print
    2 - Múltiplas declarações print

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

from aprendago.

fennder avatar fennder commented on August 22, 2024

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)

}

from aprendago.

izanf avatar izanf commented on August 22, 2024

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

from aprendago.

M3L1M avatar M3L1M commented on August 22, 2024

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

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

fmt.Println(x, y, z)

}

from aprendago.

elyosemite avatar elyosemite commented on August 22, 2024

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

from aprendago.

edlanelima avatar edlanelima commented on August 22, 2024

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

from aprendago.

an4kein avatar an4kein commented on August 22, 2024

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.

-----------------------------------------------------------------------------------------------------------------------------------------------

from aprendago.

adelsonsljunior avatar adelsonsljunior commented on August 22, 2024

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

from aprendago.

uiltonlopes avatar uiltonlopes commented on August 22, 2024

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

from aprendago.

dilandehonbra avatar dilandehonbra commented on August 22, 2024

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

from aprendago.

leonardoTavaresM avatar leonardoTavaresM commented on August 22, 2024

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

from aprendago.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.