Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exercício: Capítulo 24, Exercício 2 (Nível: 11) #74

Open
vkorbes opened this issue Sep 30, 2020 · 2 comments
Open

Exercício: Capítulo 24, Exercício 2 (Nível: 11) #74

vkorbes opened this issue Sep 30, 2020 · 2 comments

Comments

@vkorbes
Copy link
Owner

vkorbes commented Sep 30, 2020

Exercício: Capítulo 24, Exercício 2 (Nível: 11)

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!

@an4kein
Copy link

an4kein commented Mar 24, 2021

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

package main

/* - Utilizando este código: https://play.golang.org/p/9a1IAWy5E6​
- ...crie uma mensagem de erro customizada utilizando fmt.Errorf(). */

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

type person struct {
	First   string
	Last    string
	Sayings []string
}

func main() {
	p1 := person{
		First:   "James",
		Last:    "Bond",
		Sayings: []string{"Shaken, not stirred", "Any last wishes?", "Never say never"},
	}

	bs, err := toJSON(p1)
	if err != nil {
		log.Println(err)
	}
	fmt.Println(string(bs))
}

// toJSON needs to return an error also
func toJSON(a interface{}) ([]byte, error) {
	errpersonalizado := fmt.Errorf("erro personalizado")
	bs, errpersonalizado := json.Marshal(a)
	return bs, errpersonalizado
}

Output

{"First":"James","Last":"Bond","Sayings":["Shaken, not stirred","Any last wishes?","Never say never"]}

Program exited.

@an4kein
Copy link

an4kein commented Mar 24, 2021

Depois de assistir o video e ver como a Ellen fez, ficou assim

func toJSON(a interface{}) ([]byte, error) {
	//errpersonalizado := fmt.Errorf("erro personalizado")
	bs, err := json.Marshal(a)
	if err != nil {
		return []byte{}, fmt.Errorf("erros")
	}
	return bs, nil

Nao consegui entender o (return de bs, nil) nao poderia o retorno ser (return de bs, err)???

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants