Skip to content
This repository was archived by the owner on Jun 27, 2023. It is now read-only.
This repository was archived by the owner on Jun 27, 2023. It is now read-only.

Suggestion: add a var declaration to document the matched interface #1

Closed
@rickb777

Description

@rickb777

I have a suggestion that the generated mock implementation code could include a var that illustrates the implemented interface.

This has become moderately widespread idiomatic practice, mostly for the purpose of documenting a type that matches an interface. Because Go doesn't include explicit syntax matching types to interfaces (and it doesn't need to, which is a good thing), this idiomatic practice has emerged instead.

An example is most useful here. Suppose I want to mock net/http/ResponseWriter. Using mockgen, I get

// Mock of ResponseWriter interface
type MockResponseWriter struct {
    ctrl     *gomock.Controller
    recorder *_MockResponseWriterRecorder
}

My suggestion is that mockgen should also generate this:

var _ *http.ResponseWriter = &MockResponseWriter{}

it would be clear to anyone reading the code that there is an implied 'implements' relationship between the type MockResponseWriter and the interface ResponseWriter. We know this because the compiler type-checks the var declaration in a way that guarantees it to be true (given that the compiler is successful). The var introduces an unnamed variable that is never used; presumably the compiler can notice that it's unused and eliminate it from the binary code.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions