• Home
  • History
  • Annotate
Name Date Size #Lines LOC

..03-May-2022-

check/H01-Sep-2018-2,4561,951

.travis.ymlH A D01-Sep-201876 85

LICENSEH A D01-Sep-20181.5 KiB2824

README.mdH A D01-Sep-20182.1 KiB6849

main.goH A D01-Sep-2018553 3324

README.md

1# interfacer
2
3[![GoDoc](https://godoc.org/mvdan.cc/interfacer?status.svg)](https://godoc.org/mvdan.cc/interfacer)
4[![Build Status](https://travis-ci.org/mvdan/interfacer.svg?branch=master)](https://travis-ci.org/mvdan/interfacer)
5
6**Deprecated**: A tool that suggests interfaces is prone to bad suggestions, so
7its usefulness in real code is limited. This tool will remain available as a
8proof of concept, and for others to examine and learn from.
9
10A linter that suggests interface types. In other words, it warns about
11the usage of types that are more specific than necessary.
12
13	go get -u mvdan.cc/interfacer
14
15Note that this linter's suggestions tend to be subjective, as interfaces
16are not always the better option. You should select the proposed changes
17that make sense in your codebase, instead of following all of them
18blindly.
19
20### Usage
21
22```go
23func ProcessInput(f *os.File) error {
24        b, err := ioutil.ReadAll(f)
25        if err != nil {
26                return err
27        }
28        return processBytes(b)
29}
30```
31
32```sh
33$ interfacer ./...
34foo.go:10:19: f can be io.Reader
35```
36
37### Basic idea
38
39This tool inspects the parameters of your functions to see if they fit
40an interface type that is less specific than the current type.
41
42The example above illustrates this point. Overly specific interfaces
43also trigger a warning - if `f` were an `io.ReadCloser`, the same
44message would appear.
45
46It suggests interface types defined both in the func's package and the
47package's imports (two levels; direct imports and their direct imports).
48
49### False positives
50
51To avoid false positives, it never does any suggestions on functions
52that may be implementing an interface method or a named function type.
53
54It also skips parameters passed by value (excluding pointers and
55interfaces) on unexported functions, since that would introduce extra
56allocations where they are usually not worth the tradeoff.
57
58### Suppressing warnings
59
60If a suggestion is technically correct but doesn't make sense, you can
61still suppress the warning by mentioning the type in the function name:
62
63```go
64func ProcessInputFile(f *os.File) error {
65	// use as an io.Reader
66}
67```
68