Skip to content

Allocless way to check interface compatibility #33

@vbauerster

Description

@vbauerster

Interfaces part shows a way to check interface compatibility (fulfillment):

type NullWriter struct {}
var _ io.Writer = &NullWriter{}

Nothing wrong with above code, but for completeness sake it worth mention that we needn't allocate a new variable since any value of type *NullWriter will do, even nil:

type NullWriter struct {}
var _ io.Writer = (*NullWriter)(nil)

This explicit conversion works, because nil is typed in Go. Check this playground example.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions