• This repository has been archived on 01/Sep/2018
  • Stars
    star
    695
  • Rank 62,501 (Top 2 %)
  • Language
    Go
  • License
    BSD 3-Clause "New...
  • Created over 8 years ago
  • Updated over 5 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A linter that suggests interface types

interfacer

GoDoc Build Status

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

func ProcessInput(f *os.File) error {
        b, err := ioutil.ReadAll(f)
        if err != nil {
                return err
        }
        return processBytes(b)
}
$ 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:

func ProcessInputFile(f *os.File) error {
	// use as an io.Reader
}

More Repositories

1

sh

A shell parser, formatter, and interpreter with bash support; includes shfmt
Go
6,679
star
2

gofumpt

A stricter gofmt
Go
2,822
star
3

xurls

Extract urls from text
Go
1,121
star
4

github-actions-golang

GitHub Actions as CI for Go
Go
1,013
star
5

unparam

Find unused parameters in Go
Go
486
star
6

gogrep

Search for Go code using syntax trees
Go
475
star
7

fdroidcl

F-Droid desktop client
Go
239
star
8

goreduce

Reduce Go programs
Go
216
star
9

bitw

Minimalist BitWarden client
Go
159
star
10

zstd

Zstandard implementation in Wuffs
C
112
star
11

corpus

A corpus of popular Go modules
Go
100
star
12

dockexec

Run Go tests inside a Docker image
Go
96
star
13

benchinit

Benchmark the init cost of Go packages
Go
73
star
14

accesspoint

Manage wireless access points in Android (abandoned)
Java
73
star
15

pastecat

Pastebin service (abandoned)
Go
36
star
16

git-picked

List merged and cherry-picked branches
Go
29
star
17

winup

Automate a Windows 10 VM setup for coding and testing
Go
21
star
18

unindent

Report code that is unnecessarily indented
Go
18
star
19

android-template

Android app template
Java
16
star
20

dotfiles

Here be dragons
Shell
14
star
21

talks

Collection of slides from talks
Go
9
star
22

go-concurrency-workshop

Go
9
star
23

editorconfig

EditorConfig support in Go
Go
7
star
24

lint

Common interfaces for Go code checkers
Go
6
star
25

macfuzzer

Android MAC changer and randomizer (abandoned)
Java
6
star
26

gexf

GEXF file format implementation
Go
3
star
27

mvdan.cc

Personal website
HTML
3
star
28

playtyk

This repo is a temporary home for a hack.
Go
3
star
29

responsefile

Support for response files in Go, to bypass argument length limits
Go
2
star
30

tor

Consensus diffs Tor GSoC project
C
1
star
31

route

portable Go package to obtain simple routing information
Go
1
star
32

nowt

Nothing extraordinary here
Go
1
star
33

basedir

Platform-specific base directories
Go
1
star