iface

package module
v1.0.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 16, 2024 License: Apache-2.0 Imports: 0 Imported by: 0

README

Go Reference

iface

iface is a linter designed to identify the incorrect use of interfaces in Go code, helping developers avoid interface pollution. By detecting unnecessary or poorly implemented interfaces, iface ensures your Go code remains clean, efficient, and maintainable.

It consists of several analyzers :

  1. unused: Identifies interfaces that are not used anywhere in the same package where the interface is defined.
  2. identical: Identifies interfaces in the same package with identical methods or constraints.
  3. opaque: Identifies functions that return interfaces, but the actual returned value is always a single concrete implementation.

Usage

To install the linter which has all the checks:

go install github.com/uudashr/iface/cmd/ifacecheck@latest

To install individual linter, use the following command:

go install github.com/uudashr/iface/unused/cmd/unusediface@latest
go install github.com/uudashr/iface/duplicate/cmd/duplicateiface@latest
go install github.com/uudashr/iface/opaque/cmd/opaqueiface@latest

Run the linter

ifacecheck ./...

or show the help

ifacecheck help

Background

One of Go's powerful features is interfaces. However, sometimes people misuse the interfaces event though the code works but the code polluted with interfaces.

The following quotes inspired the creation of these analyzers:

"Go interfaces generally belong in the package that uses values of the interface type, not the package that implements those values. The implementing package should return concrete (usually pointer or struct) types: that way, new methods can be added to implementations without requiring extensive refactoring."

Go Code Review Comments, go.dev

"Don’t export any interfaces until you have to."

Interface pollution in Go, rakyll.org

"The use of interfaces when they are not necessary is called interface pollution."

Avoid Interface Pollution, ardanlabs.com

"Go interfaces generally belong in the package that consumes values of the interface type, not a package that implements the interface type."

Go Style Decisions, google.github.io

Documentation

Overview

Package iface provides analyzers that designed to identify the incorrect use of interfaces, helping developers avoid interface pollution.

Directories

Path Synopsis
cmd
Package identical defines an Analyzer that identifies interfaces in the same package with identical methods or constraints.
Package identical defines an Analyzer that identifies interfaces in the same package with identical methods or constraints.
Package opaque defines an Analyzer to that detects interfaces that are used to abstract a single concrete implementation only.
Package opaque defines an Analyzer to that detects interfaces that are used to abstract a single concrete implementation only.
Package unused defines an Analyzer that indetifies interfaces that are not used anywhere in the same package where the interface is defined.
Package unused defines an Analyzer that indetifies interfaces that are not used anywhere in the same package where the interface is defined.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL