
go-union
Generates Go code using a package as a union or sum type.
Given the name of a Union type T, go-union will create a new self-contained Go source file implementing
func (m T) Union() any
The file is created in the same package and directory as the package that defines T. It has helpful defaults designed
for use with go generate.
For example, given this snippet,
package painkiller
//go:generate go-union -type "Pill"
type Pill[T comparable] struct {
// This is Name doc comment
Name string // This is Name line comment
Age string
Address string `union:"-"`
NameAlias string
arrayType [5]T
funcType func()
interfaceType interface{}
mapType map[string]int
sliceType []int64
}
running this command
go-union -type "Pill"
in the same directory will create the file pill_union.go, in package painkiller, containing a definition of
// Code generated by "go-union -type Pill"; DO NOT EDIT.
// Install go-union by \"go get install github.com/searKing/golang/tools/go-union\"
package painkiller
import "reflect"
// Union switch on the variant.
func (u Pill[T]) Union() any {
if reflect.ValueOf(u.AliasStruct).IsZero() {
return u.AliasStruct
}
{
var z string
if u.Name != z {
return u.Name
}
}
{
var z int
if u.Age != z {
return u.Age
}
}
if reflect.ValueOf(u.NameAlias).IsZero() {
return u.NameAlias
}
if reflect.ValueOf(u.genericFuncType).IsZero() {
return u.genericFuncType
}
if reflect.ValueOf(u.genericStructType).IsZero() {
return u.genericStructType
}
if reflect.ValueOf(u.genericEmptyStructType).IsZero() {
return u.genericEmptyStructType
}
if u.pointerType != nil {
return u.pointerType
}
if reflect.ValueOf(u.structType).IsZero() {
return u.structType
}
{
var z [5]T
if u.arrayType != z {
return u.arrayType
}
}
if u.funcType != nil {
return u.funcType
}
if u.chanType != nil {
return u.chanType
}
{
var z any
if u.interfaceType != z {
return u.interfaceType
}
}
{
var z string
if u.stringType != z {
return u.stringType
}
}
if u.mapType != nil {
return u.mapType
}
if u.indentSliceType != nil {
return u.indentSliceType
}
if u.selectorSliceType != nil {
return u.selectorSliceType
}
if u.starSelectorSliceType != nil {
return u.starSelectorSliceType
}
return nil
}
Typically, this process would be run using go generate, like this:
//go:generate go-union -type "Pill"
If multiple constants have the same value, the lexically first matching name will be used (in the example, Acetaminophen
will print as "Paracetamol").
With no arguments, it processes the package in the current directory. Otherwise, the arguments must name a single
directory holding a Go package or a set of Go source files that represent a single Go package.
The -type flag accepts a comma-separated list of types so a single run can generate methods for multiple types. The
default output file is t_union.go, where t is the lower-cased name of the first type listed. It can be overridden with
the -output flag.
Download/Install
The easiest way to install is to run go get install github.com/searKing/golang/tools/go-union
. You can also manually git clone the repository to $GOPATH/src/github.com/searKing/golang/tools/go-union.
Inspiring projects