DEV Community

Stephen Solka
Stephen Solka

Posted on β€’ Edited on

6 1

Go errors.is cool

In 1.13 go introduced a new function on the errors package. Is. This lets you ask if the error you are holding is the same as a predefined defined error or the error wraps the predefined error.

var BadThingErr = errors.New("omg something happened")

type canReturnErr = func() error

err := canReturnErr()
if errors.Is(err, BadThingErr) {
  fmt.Println("that bad thing that can happen happened")
}
Enter fullscreen mode Exit fullscreen mode

Ive been using Is since it was released but there is a small detail of the the documentation that I originally missed.

An error is considered to match a target if it is equal to that target or if it implements a method Is(error) bool such that Is(target) returns true.

type myCoolErr struct{
  errCode int
}

func (m myCoolErr) Is(err error) bool {
  me, ok := err.(myCoolErr)
  if !ok {
    return false
  }
  return me.errCode == m.errCode
}
Enter fullscreen mode Exit fullscreen mode

This lets an error control the implementation of errors.Is when comparing equality of the error object. Neat!

How does this work?

if x, ok := err.(interface{ Is(error) bool }); ok && x.Is(target) {
  return true
}
Enter fullscreen mode Exit fullscreen mode

https://github.com/golang/go/blob/43c6ada84c6ef47e3b61646d2f2e7f6b7264929d/src/errors/wrap.go#L39-L60

In the source code of the errors package there is a cool trick. This is asking if the err object complies with an anonymous interface if it does then the go compiler allows you to call the method on the anonymous interface!

I have seen casts and casts to interfaces but never casts to an interface defined in the cast expression.

This pattern seems most useful if your target interface is a single method and only used in one place. The risk is too high for the anonymous interface definitions to diverge if its used in multiple source locations. At that point I'd recommend defining a proper type Xxx interface

Image of AssemblyAI tool

Transforming Interviews into Publishable Stories with AssemblyAI

Insightview is a modern web application that streamlines the interview workflow for journalists. By leveraging AssemblyAI's LeMUR and Universal-2 technology, it transforms raw interview recordings into structured, actionable content, dramatically reducing the time from recording to publication.

Key Features:
πŸŽ₯ Audio/video file upload with real-time preview
πŸ—£οΈ Advanced transcription with speaker identification
⭐ Automatic highlight extraction of key moments
✍️ AI-powered article draft generation
πŸ“€ Export interview's subtitles in VTT format

Read full post

Top comments (0)

Heroku

Simplify your DevOps and maximize your time.

Since 2007, Heroku has been the go-to platform for developers as it monitors uptime, performance, and infrastructure concerns, allowing you to focus on writing code.

Learn More

πŸ‘‹ Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay