
Union Golang is a proposal that aims to bring the concept of unions to the Go programming language. The proposal was submitted by the Go team in 2022.
Unions are a fundamental data type in many programming languages, allowing a variable to hold one of several different types of values. The Go team has been exploring ways to add this feature to Go.
The proposal suggests using the `|` operator to define a union type, as shown in the example `type Point | Rectangle struct { ... }`. This operator is already used in Go for bit operations, but in this context, it would be used to specify multiple types within a single union type.
The proposed implementation would allow developers to use unions in a flexible and efficient way, making it easier to work with complex data structures.
Readers also liked: Golang Go
Syntax and Usage
To inspect the dynamic state of a union-typed value, you can use union assertions, which are similar to type assertions on interfaces. This involves using a primary expression that can be a constant or type in the discriminator set of the union type, or literal nil to indicate the nil case.
Suggestion: Golang Types
Union assertions can be used in contexts expecting one or two values. If there's only one result, the assertion panics if the discriminator is not equal or identical to the expression. If there are two, the first is the dependent value if the discriminator is equal or identical and the second is a bool indicating whether the assertion succeeded.
You can also use switch statements that mirror type switches, called discriminator switches, to inspect the dynamic state of a union-typed value. The expressions in the case clauses in a discriminator switch must each be in the discriminator set of the union type of the primary expression of the guard.
Proposal
Sigma types are a central feature of dependent type systems, allowing for statically proving many facts about the safety of programs. They enable type metaprogramming, which is not possible in Go without this feature.
I propose the introduction of a restricted version of sigma types into Go, where a must be one of a statically enumerated set of values. This restriction is necessary due to Go's lack of metaprogramming.
Underlying type terms are being considered as proper types, allowing for a "shortcut" for functions that are generic over a single type per formal parameter. This could potentially simplify the process of creating generic functions.
Syntax

Sigma types are pairs (a: T, b: B(a)) where a is a value of type T and B is a function that constructs the type of b from the value of a.
In this syntax, a and b are closely tied together through the function B, which creates the type of b based on the value of a.
A is a value of type T, and B is a function that constructs the type of b from the value of a. This is a fundamental aspect of sigma types.
The restriction is that a must be one of a statically enumerated set of values, meaning it can't depend on variable values. This is a deliberate choice to make sigma types work in Go.
Sigma types can be thought of as tagged unions where the tag is an accessible value of a type the programmer can select. This makes them useful for certain use cases.
You might like: T Golang
Inspecting

Inspecting union-typed values is a straightforward process. You can use syntax similar to interface types to inspect the dynamic state of a union-typed value.
Union assertions are a primary expression that can be used in contexts expecting one or two values. They can be used to assert the discriminator of a union type, and will panic if the discriminator is not equal or identical to the expression.
Discriminator switches are another way to inspect union-typed values. They mirror type switches and can be used to handle different cases of a union type. The expressions in the case clauses must be in the discriminator set of the union type.
You can also use the special form x.(switch) to evaluate the current value of the discriminator of a value-discriminated union type. This syntax is the same as the guard of a discriminator switch, and requires that the case values be members of the discriminator set.
Using a type switch can be a useful way to discover the dynamic type of an interface variable, especially for sum types.
Recommended read: Golang Use Cases
Properties

The properties of union types are quite interesting. The zero value of a value-discriminated union type has the zero value of the discriminator type as its discriminator and the zero value of the dependent type as its dependent value.
For type-discriminated union types, the zero value has nil as its discriminator and the zero value of the dependent type of the nil case as its dependent value. This can be a bit tricky to wrap your head around, but essentially it's the default value for a union type.
Operations that can be used with all dependent types of a union type can also be used with the union type itself. This means that you can use the same operations on the union type that you would on its dependent values. This property is useful for mirroring the semantics of type elements in generic functions.
Union types are comparable when their discriminators are equal or identical, and their dependent values are equal. This is a straightforward way to determine if two union types are comparable.
The alignment requirement of a union type is the maximum of the alignments of all dependent types and the discriminator type. This means that the union type will be aligned according to the most restrictive alignment requirement of its dependent types and the discriminator type.
Sum Variant Type and Switch Case Static Check in Go

You can use sum types, also known as interface types, in Go to represent sum types effectively.
To work with sum types, you can use "interface" to represent them, and then use basic ways to work with interface.
The zero value of a value-discriminated union type has the zero value of the discriminator type as its discriminator and the zero value of the dependent type as its dependent value.
In order to inspect the dynamic state of a union-typed value, you can use union assertions, a primary expression.
Union assertions can be used in contexts expecting one or two values, and when there is only one result, the assertion panics if the discriminator is not equal or identical to the expression.
You can use switch statements that mirror type switches, called discriminator switches, to inspect the dynamic state of a union-typed value.
The expressions in the case clauses in a discriminator switch must each be in the discriminator set of the union type of the primary expression of the guard.
A different take: Golang Interface
You can use the special form x.(switch) to evaluate to the current value of the discriminator.
The gosumcheck tool can be used to statically check all cases are handled in type-switch statements.
The idea behind gosumcheck is to gather all possible types that implement a given interface and output missing types that are not handled by "case" statements.
You can use gosumcheck for both "public" and "internal" interfaces, but it's more suited for "internal" interfaces where all types that implement the interface are fixed and in one package.
The gosumcheck tool can also be used as a linter to list up possible types that implement a given interface, even if they are in external packages.
Handling Ambiguity
A minor syntactic ambiguity can arise with union type literals in Go. This occurs when a union type literal without a type name is written in a statement context.
The parser assumes a switch statement when encountering the switch keyword in statement context. This is because it's unlikely that such an expression would ever arise in practice.
To write a union type literal in statement context, you can wrap at least the type in parentheses. This is a workaround for the awkwardness and uselessness of the expression.
Standard Library Support
The standard library support for union types in Go is expanding. A new Union value is being added to the Kind type, and a new type DependentType is being introduced.
A DependentType has methods NumDependent and Dependent, which return the number of discriminators on a type and a DependentType according to an implementation-defined enumeration, respectively.
Union types will also get a Discriminator method that returns the type of the discriminator, and a DiscriminatorTag method that returns the tag of the discriminator.
On value-discriminated union types, a new Discriminator method will return the current discriminator as a Value | Type, and the existing Elem method will return the dependent value.
Examples and Usage
In Go, you can use interfaces to represent sum types, which is a basic way to work with them.
To use sum types effectively in Go, you need to know how to work with interfaces.
Sum types can be used to represent multiple types in a single variable, allowing for more flexible and expressive code.
For example, you can use an interface to represent a sum type by defining a type that implements multiple interfaces. This is demonstrated in the section "How to use sum types (, or interface type) in Go".
Using interfaces to represent sum types can be a powerful tool in your Go development toolkit.
By understanding how to work with interfaces and sum types, you can write more efficient and effective code in Go.
Type Switch and Decoding
You can use a type switch to discover the dynamic type of an interface variable. Type assertion is also an option, but for sum types, a type switch might be more useful in most cases.
The article provides an example of using a type switch to work with sum types. It mentions that you can use the go/ast/#Inspect package to inspect the type of an interface variable. Type switches are particularly useful when working with sum types, as they allow you to handle different types of data in a more elegant way.
To demonstrate this, let's take a look at the example provided in the article. It shows how to use a type switch to decode JSON data that contains a "type" field. This is a common pattern in REST APIs, where the type of data is encoded as a string.
Here's a step-by-step breakdown of how to use a type switch to decode JSON data:
1. Decode the JSON data to a list of json.RawMessage.
2. Use a type switch to determine the type of each json.RawMessage.
3. Decode the json.RawMessage to the corresponding type based on the type switch.
This approach can be more efficient than using a type assertion, especially when working with large amounts of data. It also makes the code more readable and maintainable, as the type switch clearly indicates the type of data being handled.
In the article, the author provides a benchmark comparison between using a type switch and a type assertion to decode JSON data. The results show that the type switch approach is faster and more efficient, making it a better choice for production code.
By using a type switch to handle sum types, you can write more robust and maintainable code that scales well with large datasets. It's a powerful technique that's worth mastering, especially when working with complex data structures.
Suggestion: S Golang
Algorithm and Method
In Golang, finding the union of two unsorted arrays can be achieved using a set data structure with a time complexity of O(n + m), where n and m are the size of the arrays.
To start, we need to create a package main and declare the fmt package, which helps with formatting input and output.
A key step in the algorithm is to create a main function, where we define two slices, slice1 and slice2, and print them on the console.
The union of elements is achieved by calling the function union_ele with slice1 and slice2 as parameters.
Here's a step-by-step breakdown of the algorithm:
- Create a package main and declare fmt package.
- Create a main function and define two slices, slice1 and slice2.
- Call the function union_ele with slice1 and slice2 as parameters.
- Create an empty map to store the elements of the union using the make function.
- Create a slice named output to store the union of elements using the make function.
- Run a loop to check each element in slice1 and add it to the map and output slice if it doesn't exist.
- Similarly, repeat the same process with slice2 and add it to the output slice.
- Return the output to the function and print it on the console using the fmt.Println function.
Featured Images: pexels.com


