linter err113: do not define dynamic errors, use wrapped static errors instead

Declare a package-level variables as suggested:

 var repoGaveErr = errors.New("repo gave err")

 func someFunc() {
    repoMock.EXPECT().Save(gomock.Eq(&foooBarBar)).Return(nil, repoGaveErr)
 }

Every call to errors.New allocates a new unique error value. The application creates a single value representing the error by declaring the package-level variable.

There are two motivations for the single value:

  • The application can compare values for equality to check for a specific error condition.

  • Reduce memory allocations (although probably not a big deal in practice)

The value io.EOF is a canonical example.

Tags:

Go