davideisinger.com

My personal website
Log | Files | Refs | README

commit b19b7f6717c20743f01613ec932f27559b774e80
parent d18745e8c2c1a4460c939a2956929f803f50599d
Author: David Eisinger <[email protected]>
Date:   Sat, 15 Jul 2023 23:54:30 -0400

custom error types

Diffstat:
Mcontent/notes/golang/index.md | 4++++
1 file changed, 4 insertions(+), 0 deletions(-)

diff --git a/content/notes/golang/index.md b/content/notes/golang/index.md @@ -112,7 +112,11 @@ I find [Go][1] really compelling, even though it's not super applicable to my jo * Built-in functions for conversion (`float64`, `strconv.Atoi`) * `if v, ok := fnb.(FancyNumber); ok {` (`v` is a `FancyNumber` if `ok` is true) * `switch v := i.(type) {` (case per type, `v` is `i` cast to that type) +* [Custom error types][19] + +> Usually, a struct is used to create a custom error type. By convention, custom error type names should end with `Error`. Also, it is best to set up the `Error() string` method with a pointer receiver, see this [Stackoverflow comment](https://stackoverflow.com/a/50333850) to learn about the reasoning. Note that this means you need to return a pointer to your custom error otherwise it will not count as `error` because the non-pointer value does not provide the `Error() string` method. [17]: https://stackoverflow.com/a/40951013 [18]: https://go.dev/doc/effective_go#named-results +[19]: https://exercism.org/tracks/go/concepts/errors