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 4 (Nível: 11) #76

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

Exercício: Capítulo 24, Exercício 4 (Nível: 11) #76

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 4 (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/21DbEstR0_q

package main

/* - Utilizando este código: https://play.golang.org/p/wlEM1tgfQD​
- ...use o struct sqrt.Error como valor do tipo erro. */

import (
	"fmt"
	"log"
)

type sqrtError struct {
	lat  string
	long string
	err  error
}

func (se sqrtError) Error() string {
	return fmt.Sprintf("math error: lat: %v | long: %v | valor: %v", se.lat, se.long, se.err)
}

func main() {
	test, err := sqrt(-10.23)
	if err != nil {
		log.Println(err)
	}
	fmt.Println(test)
}

func sqrt(f float64) (float64, error) {
	fmt.Println("esse eh o valor de f:", float64(f))
	if f < 0 {
		return 0, sqrtError{lat: "1337", long: "1338", err: fmt.Errorf("%v", f)}
	}
	return 42, nil
}

Output

eduardo@ubuntu:~/newCodes/04$ go run main.go 
esse eh o valor de f: -10.23
2021/03/24 14:44:53 math error: lat: 1337 | long: 1338 | valor: -10.23
0

@UdsonWillams
Copy link

package main

import (
	"errors"
	"fmt"
	"log"
)

type sqrtError struct {
	lat  string
	long string
	err  error
}

func (se sqrtError) Error() string {
	return fmt.Sprintf("math error: %v - %v - %v", se.lat, se.long, se.err)
}

func main() {
	_, err := sqrt(-10.23)
	if err != nil {
		log.Println(err)
	}
}

func sqrt(f float64) (float64, error) {
	if f < 0 {
		return f, sqrtError{
			lat:  "10",
			long: "21",
			err:  errors.New("Valor passado menor que 0"),
		}
	}
	return 42, nil
}

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

3 participants