#How do I make errors?

34 messages · Page 1 of 1 (latest)

unborn cape
#

Let's say I want to decode a PNG, but want to return an error when something in the file has an error. How do I do that?

#

Just saw the errors package existing

#

Interesting

plain lava
#

error is an interface, so you can return any type that has a method of Error() string on it. that said, there are some helper functions that exist to easily create one, like errors.New or fmt.Errorf, that take in a string and return a value that already has that method implemented for you

#

if you want to take an existing error and add more context to it, you can "wrap" it with the %w verb on fmt.Errorf. this allows the consumer of the error to both have that new context but also retrieve the inner error by unwrapping it

unborn cape
#

Uh, as a bit of a newbie, I'm not sure I fully understand what an interface is.

plain lava
unborn cape
#

oh, perfect

#

one moment

plain lava
#

an interface is just a set of methods, and any type that implements all of those methods is said to implement (or satisfy) the interface. then, you can use that type wherever the interface type is expected

#

the definition of error is:

type error interface {
    Error() string
}

meaning that any type that has a method with the signature Error() string satisfies error, and any value of one of those types can be used wherever an error is wanted

unborn cape
#

Not sure I understand, sorry.

#

The tour page wasn't very helpful

plain lava
#

notice how a can take refer to values of different types, because they both implement the Abser interface

#

at first it refers to a MyFloat, then it refers to a *Vertex

unborn cape
#

Because Abser is a float64?

#

or sorry

plain lava
#

no, Abser is an interface

unborn cape
#

And MyFloat and *Vertex implement Abser why?

plain lava
#

we'd say they implement Abser, not that they are implemented by it

unborn cape
#

sure

plain lava
#

they both have an Abs() float64 method, see lines 29 and 40

#

if you look at the definition of Abser, that is precisely what is required to implement Abser

unborn cape
#

ohhh. That makes a lot of sense

#

So why have Abser and not just Abs?

#

why not have a be a float64?

plain lava
#

because there are multiple types of mathematical constructs that have the absolute value operation defined on them

#

one is a float, but another is a vertex

#

having Abser allows logic using an absolute value to be written only once for both types (and even more types)

unborn cape
#

I think I have a ton to learn lol. Is there a master guide to how Go works? I've tried the Go tour but I didn't really learn much from it.

plain lava
#

the tour is the canonical set of resources for beginners. i would suggest going through it again and identifying where you feel like there's a gap in knowledge before moving on, and if you're not able to put things together i'm sure anyone around here would be happy to help

#

it's easy to just go to the next page when something doesn't make much sense, but try to resist that

unborn cape
#

fair lol

#

Alright, back to it I guess