Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/mvdan/interfacer
A linter that suggests interface types
https://github.com/mvdan/interfacer
go interface linter types
Last synced: 3 months ago
JSON representation
A linter that suggests interface types
- Host: GitHub
- URL: https://github.com/mvdan/interfacer
- Owner: mvdan
- License: bsd-3-clause
- Archived: true
- Created: 2015-12-06T02:27:57.000Z (almost 9 years ago)
- Default Branch: master
- Last Pushed: 2018-09-01T00:41:13.000Z (about 6 years ago)
- Last Synced: 2024-06-19T12:44:40.575Z (5 months ago)
- Topics: go, interface, linter, types
- Language: Go
- Homepage:
- Size: 543 KB
- Stars: 692
- Watchers: 12
- Forks: 17
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-go - interfacer - A linter that suggests interface types - ★ 717 (Code Analysis)
README
# interfacer
[![GoDoc](https://godoc.org/mvdan.cc/interfacer?status.svg)](https://godoc.org/mvdan.cc/interfacer)
[![Build Status](https://travis-ci.org/mvdan/interfacer.svg?branch=master)](https://travis-ci.org/mvdan/interfacer)**Deprecated**: A tool that suggests interfaces is prone to bad suggestions, so
its usefulness in real code is limited. This tool will remain available as a
proof of concept, and for others to examine and learn from.A linter that suggests interface types. In other words, it warns about
the usage of types that are more specific than necessary.go get -u mvdan.cc/interfacer
Note that this linter's suggestions tend to be subjective, as interfaces
are not always the better option. You should select the proposed changes
that make sense in your codebase, instead of following all of them
blindly.### Usage
```go
func ProcessInput(f *os.File) error {
b, err := ioutil.ReadAll(f)
if err != nil {
return err
}
return processBytes(b)
}
``````sh
$ interfacer ./...
foo.go:10:19: f can be io.Reader
```### Basic idea
This tool inspects the parameters of your functions to see if they fit
an interface type that is less specific than the current type.The example above illustrates this point. Overly specific interfaces
also trigger a warning - if `f` were an `io.ReadCloser`, the same
message would appear.It suggests interface types defined both in the func's package and the
package's imports (two levels; direct imports and their direct imports).### False positives
To avoid false positives, it never does any suggestions on functions
that may be implementing an interface method or a named function type.It also skips parameters passed by value (excluding pointers and
interfaces) on unexported functions, since that would introduce extra
allocations where they are usually not worth the tradeoff.### Suppressing warnings
If a suggestion is technically correct but doesn't make sense, you can
still suppress the warning by mentioning the type in the function name:```go
func ProcessInputFile(f *os.File) error {
// use as an io.Reader
}
```