vkorbes / aprendago

Curso completo em português da linguagem Go, de zero a ninja! 🇧🇷
http://aprendago.com
1.05k stars 184 forks source link

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

Open vkorbes opened 4 years ago

vkorbes commented 4 years ago

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!

diegoparra commented 4 years ago

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

an4kein commented 3 years ago

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.
alansantosmg commented 3 years ago
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")
    }

}
wfrsilva commented 2 years ago

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

image

Harsgaard commented 1 year ago

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

LelecoNN commented 1 year ago

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

}
LeandroCGMS commented 6 months ago

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