aprendago icon indicating copy to clipboard operation
aprendago copied to clipboard

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

Open vkorbes opened this issue 4 years ago • 7 comments

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

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!

vkorbes avatar Sep 30 '20 17:09 vkorbes

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

diegoparra avatar Oct 16 '20 00:10 diegoparra

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

package main

import (
	"encoding/json"
	"fmt"
	"os"
)

/* - Partindo do código abaixo, utilize NewEncoder() e Encode() para enviar as informações como JSON para Stdout.
    - https://play.golang.org/p/BVRZTdlUZ_​
- Desafio: descubra o que é, e utilize, method chaining para conectar os dois métodos acima. */

type user struct {
	First   string
	Last    string
	Age     int
	Sayings []string
}

func main() {
	u1 := user{
		First: "James",
		Last:  "Bond",
		Age:   32,
		Sayings: []string{
			"Shaken, not stirred",
			"Youth is no guarantee of innovation",
			"In his majesty's royal service",
		},
	}

	u2 := user{
		First: "Miss",
		Last:  "Moneypenny",
		Age:   27,
		Sayings: []string{
			"James, it is soo good to see you",
			"Would you like me to take care of that for you, James?",
			"I would really prefer to be a secret agent myself.",
		},
	}

	u3 := user{
		First: "M",
		Last:  "Hmmmm",
		Age:   54,
		Sayings: []string{
			"Oh, James. You didn't.",
			"Dear God, what has James done now?",
			"Can someone please tell me where James Bond is?",
		},
	}

	users := []user{u1, u2, u3}

	// your code goes here

	// func NewEncoder(w io.Writer) *Encoder
	// https://gobyexample.com/json

	/* meujson := json.NewEncoder(os.Stdout)
	meujson.Encode(users) */
	// https://www.youtube.com/watch?v=Q4sYrKFJqPo --> Method Chaining Explained (in Computer Programming)
	err := json.NewEncoder(os.Stdout).Encode(users)
	if err != nil {
		fmt.Println("Error:", err)
	}
	//meujson.Encode(users)

}

Output

[{"First":"James","Last":"Bond","Age":32,"Sayings":["Shaken, not stirred","Youth is no guarantee of innovation","In his majesty's royal service"]},{"First":"Miss","Last":"Moneypenny","Age":27,"Sayings":["James, it is soo good to see you","Would you like me to take care of that for you, James?","I would really prefer to be a secret agent myself."]},{"First":"M","Last":"Hmmmm","Age":54,"Sayings":["Oh, James. You didn't.","Dear God, what has James done now?","Can someone please tell me where James Bond is?"]}]

Program exited.

an4kein avatar Mar 02 '21 20:03 an4kein

package main

import (
	"encoding/json"
	"fmt"
	"os"
)

type user struct {
	First   string
	Last    string
	Age     int
	Sayings []string
}

func main() {
	u1 := user{
		First: "James",
		Last:  "Bond",
		Age:   32,
		Sayings: []string{
			"Shaken, not stirred",
			"Youth is no guarantee of innovation",
			"In his majesty's royal service",
		},
	}

	u2 := user{
		First: "Miss",
		Last:  "Moneypenny",
		Age:   27,
		Sayings: []string{
			"James, it is soo good to see you",
			"Would you like me to take care of that for you, James?",
			"I would really prefer to be a secret agent myself.",
		},
	}

	u3 := user{
		First: "M",
		Last:  "Hmmmm",
		Age:   54,
		Sayings: []string{
			"Oh, James. You didn't.",
			"Dear God, what has James done now?",
			"Can someone please tell me where James Bond is?",
		},
	}

	users := []user{u1, u2, u3}

	//fmt.Println(users)

	err := json.NewEncoder(os.Stdout).Encode(users)
	if err != nil {
		fmt.Println("Erro de encoding")
	}

}

alansantosmg avatar Apr 26 '21 01:04 alansantosmg

Cap. 17 – Exercícios: Nível #8 – 3 https://go.dev/play/p/Z-_7Elwot14

image

wfrsilva avatar Jun 01 '22 00:06 wfrsilva

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

Harsgaard avatar Dec 18 '22 06:12 Harsgaard

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

package main

import (
	"encoding/json"
	"fmt"
	"os"
)

type user struct {
	First   string
	Last    string
	Age     int
	Sayings []string
}

func main() {
	us := []user{
		user{First: "James", Last: "Bond", Age: 32, Sayings: []string{
			"Shaken, not stirred", "Youth is no guarantee of innovation", "In his majesty's royal service"}},
		user{First: "Miss", Last: "Moneypenny", Age: 27, Sayings: []string{
			"James, it is soo good to see you", "Would you like me to take care of that for you, James?",
			"I would really prefer to be a secret agent myself."}},
		user{First: "M", Last: "Hmmmm", Age: 54, Sayings: []string{
			"Oh, James. You didn't.", "Dear God, what has James done now?",
			"Can someone please tell me where James Bond is?"}},
	}
	//usar method chaining
	er := json.NewEncoder(os.Stdout).Encode(us)
	//er := uss.Encode(us)
	if er != nil {
		fmt.Println("deu erros", er)
	}

	// your code goes here

}

LelecoNN avatar Oct 10 '23 14:10 LelecoNN

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

LeandroCGMS avatar May 11 '24 22:05 LeandroCGMS