Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Accept Interfaces, Return Concrete Types

License

NotificationsYou must be signed in to change notification settings

butuzov/ireturn

Use this GitHub action with your project
Add this Action to an existing workflow or create a new one
View on Marketplace
 
 

Repository files navigation

Accept Interfaces, Return Concrete Types


United 24


Install

You can getireturn withgo install command. Go1.23+ required.

go install github.com/butuzov/ireturn/cmd/ireturn@latest

Compiled Binary

Or you can download the suitable binary from thereleases 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" forinterface{},anon for anonymous interfaces):

# 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 onesireturn --reject="std,github.com/shipt/plinko.Payload" ./...# default settings allows errors, empty interfaces, anonymous declarations and standard libraryireturn ./...# checkfor non idiomatic interface namesireturn -allow="error,generic,anon,stdlib,.*(or|er)$" ./...

Keywords

You can use shorthand for some types of interfaces:

  • empty forinterface{} type
  • anon for anonymous declarationsinterface{ someMethod() }
  • error forerror 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 withireturn

GitHub Action

- uses: butuzov/ireturn-linter@main  with:    allow: "error,empty"

Examples

// Bad.typeDoerinterface {Do() }typeIDoerstruct{}funcNew()Doer {returnnew(IDoer)}func (d*IDoer)Do() {/*...*/}// Good.typeDoerinterface {Do() }typeIDoerstruct{}funcNew()*IDoer {returnnew(IDoer)}func (d*IDoer)Do() {/*...*/}// Very Good (Verify Interface Compliance in compile time)var_Doer= (*IDoer)(nil)typeDoerinterface {Do() }typeIDoerstruct{}funcNew()*IDoer {returnnew(IDoer)}func (d*IDoer)Do() {/*...*/}

Reading List

About

Accept Interfaces, Return Concrete Types

Topics

Resources

License

Stars

Watchers

Forks

Contributors9


[8]ページ先頭

©2009-2025 Movatter.jp