I googled this and found several explanations, such as https://stackoverflow.com/questions/72267243/unioning-an-interface-with-a-type-in-golang#:~:text=2%20Answers,~%20)%20and%20fmt.Stringer%20 .
It's also unclear to me why you think using "any" would be less safe. You still have to use a type-assertion or type-switch to disambiguate the two cases. If the compiler allowed your example it might catch obvious mistakes at compile time but you still have to hand write code to detect at run-time which case is in effect. And such code should always have a "Oops! Got the wrong type" path. On Fri, Mar 13, 2026 at 7:52 PM Pierre Durand <[email protected]> wrote: > I'm trying to define a type constraint which is a union of interface. > > type ReadStringer interface { > io.Reader | fmt.Stringer > } > > Unfortunately the compiler rejects it: > cannot use io.Reader in union (io.Reader contains methods) > cannot use fmt.Stringer in union (fmt.Stringer contains methods) > > I understand it's not possible, but I don't understand why. > Is there a logical problem ? Could it be supported in a future Go version ? > > My goal is to define a function that accepts a parameter, then check at > the execution time if it implements an interface or the other. > I could use "any", but it would be less safe. > > Thank you. > > -- > You received this message because you are subscribed to the Google Groups > "golang-nuts" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to [email protected]. > To view this discussion visit > https://groups.google.com/d/msgid/golang-nuts/375e05d7-151a-4cd9-bb60-bc617aa20bebn%40googlegroups.com > <https://groups.google.com/d/msgid/golang-nuts/375e05d7-151a-4cd9-bb60-bc617aa20bebn%40googlegroups.com?utm_medium=email&utm_source=footer> > . > -- Kurtis Rader Caretaker of the exceptional canines Junior and Hank -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To view this discussion visit https://groups.google.com/d/msgid/golang-nuts/CABx2%3DD-ZPGwa7usqP7iYQRTo37Fmq_HNK%3D1DnRCQ8Wv-a5weKg%40mail.gmail.com.
