Go 1.24 Introduces Generic Type Alias Support
The article explains how Go 1.24 finally enables type aliases to have their own generic type parameters, provides background on the previous limitation, demonstrates usage with concrete code examples, and notes the experimental flag required in Go 1.23.
Since Go 1.18 introduced generics, many developers expected a major shift, but the feature is still evolving. Go 1.24 now adds a new capability: allowing type aliases to declare their own type parameters.
Background
The original generics proposal stated that a type alias can reference a generic type, but the alias itself cannot have its own parameters because handling constrained type parameters was unclear.
Previously, declaring an alias with a generic parameter such as type A[P any] = P was illegal and resulted in a compilation error.
Go 1.24: Generic Support for Type Aliases
After years of discussion, Go 1.24 (scheduled for release in February) will permit type aliases to have type parameters.
Example usage:
// Define a generic slice type
type GenericSlice[T any] = []T
// Example: custom struct slice
type Fish struct {
Name string
IsGrilled bool
Ingredients []string
}
func main() {
// Generic slice of strings
words := GenericSlice[string]{"Go", "Rocks"}
fmt.Println("Words:", words)
// Generic slice of Fish structs
fishes := GenericSlice[Fish]{
{Name: "Salmon", IsGrilled: true, Ingredients: []string{"Salt", "Lemon", "Garlic"}},
{Name: "Tuna", IsGrilled: false, Ingredients: []string{"Soy Sauce", "Wasabi"}},
}
fmt.Println("Grilled Fish Menu:", fishes)
}Running this program produces:
Words: [Go Rocks]
Grilled Fish Menu: [{Salmon true [Salt Lemon Garlic]} {Tuna false [Soy Sauce Wasabi]}]On Go 1.23 the same code fails with the error:
./prog.go:6:6: generic type alias requires GOEXPERIMENT=aliastypeparamsThis is because the feature was experimental in Go 1.23 and becomes a standard part of the language in Go 1.24.
Summary
Since generics were officially added in Go 1.18, the language has been iteratively refined. The new support for generic type aliases, a feature that took nearly four years to land, marks another step forward, though developers may still encounter rough edges.
For further reading, see the linked articles on other Go 1.24 features.
IT Services Circle
Delivering cutting-edge internet insights and practical learning resources. We're a passionate and principled IT media platform.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.