神刀安全网

interfacer – suggest interface types in Go

interfacer

interfacer – suggest interface types in Go

A linter that suggests interface types. In other words, it warns about the usage of types that are more specific than necessary.

go get github.com/mvdan/interfacer/cmd/interfacer 

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 package relies on go/types for the heavy lifting: name resolution, constant folding and type inference. It also uses go/loader to resolve the packages specified by import paths.

It 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 std and in your packages. To avoid false positives, it never does any suggestions on functions that may be implementing an interface method or a named function type.

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 }

Caveats

  • Vendor support only on 1.6 or later. The necessary parts were not added to go/build until 1.6.

转载本站任何文章请注明:转载至神刀安全网,谢谢神刀安全网 » interfacer – suggest interface types in Go

分享到:更多 ()

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
分享按钮