Type that can hold two interfaces with no commonalities


in a program I’m writing I have (among others) two interface types, which share no common methods.

Let’s call them A and B

type A interface { /* Method set of A */ }
type A interface { /* Method set of B */ }

I now need a type that can hold either one of which.

The first thing that came into my mind is an empty interface, and to then use a type assertion, to determine whether I have A or B.

Is there any way to avoid using the empty interface here? I’m kind of reluctant to use it, because I’d loose static checks.

You can define an interface with a non-exported function that you never use:

type AorB interface {

type A struct { /* ... */ }

func (A) aorb() {}

type B struct { /* ... */ }

func (B) aorb() {}


1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.