Union restrictions

May 4, 2023

General interfaces

Implementation restriction: A union (with more than one term) cannot contain the predeclared identifier comparable or interfaces that specify methods, or embed comparable or interfaces that specify methods.

Or putting this another way: Unions (that is, interface type parameters with a | character), only work on interfaces defined by other type parameters.

interface {
  int | float32    // Valid
  int | error      // Invalid: error is an interface that specifies a method
  int | comparable // Invalid: comparable is not a type parameter-defined interface
}

Quotes from _The Go Programming Language Specification_ Version of December 15, 2022

Share this

Direct to your inbox, daily. I respect your privacy .

Unsure? Browse the archive .

Get daily content like this in your inbox!

Subscribe