https://github.com/butuzov/ireturn
Accept Interfaces, Return Concrete Types
https://github.com/butuzov/ireturn
go golangci-lint interfaces linter
Last synced: 12 months ago
JSON representation
Accept Interfaces, Return Concrete Types
- Host: GitHub
- URL: https://github.com/butuzov/ireturn
- Owner: butuzov
- License: mit
- Created: 2021-08-18T17:47:53.000Z (over 4 years ago)
- Default Branch: main
- Last Pushed: 2025-05-05T00:32:17.000Z (12 months ago)
- Last Synced: 2025-05-15T15:01:58.388Z (12 months ago)
- Topics: go, golangci-lint, interfaces, linter
- Language: Go
- Homepage:
- Size: 142 KB
- Stars: 66
- Watchers: 3
- Forks: 4
- Open Issues: 7
-
Metadata Files:
- Readme: readme.md
- License: LICENSE
Awesome Lists containing this project
README
# `ireturn` [](https://u24.gov.ua/) [](https://coveralls.io/github/butuzov/ireturn?branch=main) []() [](http://www.opensource.org/licenses/MIT)
Accept Interfaces, Return Concrete Types
---
[](https://u24.gov.ua/)
---
## Install
You can get `ireturn` with `go install` command. Go1.23+ required.
```shell
go install github.com/butuzov/ireturn/cmd/ireturn@latest
```
### Compiled Binary
Or you can download the suitable binary from the [releases](https://github.com/butuzov/ireturn/releases) section.
## Usage
`ireturn` work with two arguments (but allow to use of only one of them in the same moment):
- `accept` - accept-list of the comma-separated interfaces.
- `reject` - reject-list of the comma-separated interfaces.
By default, `ireturn` will accept all errors (`error`), empty interfaces (`interfaces{}`), anonymous interfaces declarations ( `interface { methodName() }` ) and interfaces from standard library as a valid ones.
Interfaces in the list can be provided as regexps or keywords ("error" for "error", "empty" for `interface{}`, `anon` for anonymous interfaces):
```bash
# allow usage of empty interfaces, errors and Doer interface from any package.
ireturn --accept="\\.Doer,error,empty" ./...
# reject standard library interfaces and plinko.Payload as valid ones
ireturn --reject="std,github.com/shipt/plinko.Payload" ./...
# default settings allows errors, empty interfaces, anonymous declarations and standard library
ireturn ./...
# checkfor non idiomatic interface names
ireturn -allow="error,generic,anon,stdlib,.*(or|er)$" ./...
```
### Keywords
You can use shorthand for some types of interfaces:
- `empty` for `interface{}` type
- `anon` for anonymous declarations `interface{ someMethod() }`
- `error` for `error` type
- `stdlib` for all interfaces from standard library.
- `generic` for generic interfaces (added in go1.18)
### Disable directive
`golangci-lint` compliant disable directive `//nolint:ireturn` can be used with `ireturn`
### GitHub Action
```
- uses: butuzov/ireturn-linter@main
with:
allow: "error,empty"
```
## Examples
```go
// Bad.
type Doer interface { Do() }
type IDoer struct{}
func New() Doer { return new(IDoer)}
func (d *IDoer) Do() {/*...*/}
// Good.
type Doer interface { Do() }
type IDoer struct{}
func New() *IDoer { return new(IDoer)}
func (d *IDoer) Do() {/*...*/}
// Very Good (Verify Interface Compliance in compile time)
var _ Doer = (*IDoer)(nil)
type Doer interface { Do() }
type IDoer struct{}
func New() *IDoer { return new(IDoer)}
func (d *IDoer) Do() {/*...*/}
```
## Reading List
- [Rob Pike's comment on "Accept Interfaces Return Struct in Go"](https://github.com/go-proverbs/go-proverbs.github.io/issues/37)
- [Accept Interfaces Return Struct in Go](https://mycodesmells.com/post/accept-interfaces-return-struct-in-go)
- [Accept Interface Return Struct](https://blog.dlow.me/programming/golang/accept-interface-return-struct/)
- [What “accept interfaces, return structs” means in Go](https://medium.com/@cep21/what-accept-interfaces-return-structs-means-in-go-2fe879e25ee8)