From Functions to Types: Mastering Go’s Generic Syntax

Before We Begin
As is well known, current AI systems such as GPT and Gemini are better suited for assisted learning. With that in mind, let’s continue exploring Go generics together. If there are any mistakes or omissions, feedback and corrections are very welcome.
Preface
With the release of Go v1.18, the language introduced parametric polymorphism, commonly referred to as generics. This feature allows developers to write code that is not tied to a specific type, using type parameters as placeholders. The syntax utilizes square brackets [] to define these parameters, a choice made to avoid ambiguity with channels and multiple variable assignments often associated with the <> syntax found in other languages.
1. Declaring Generic Functions
A generic function is defined by placing a type parameter list in square brackets between the function name and its regular parameters. Each type parameter must have a constraint, which is an interface type that defines which types are permitted as arguments.
Syntax Example: Generic Function
1 | // T is a type parameter constrained by 'any' |
In this example, any is a predeclared alias for an empty interface (interface{}), meaning any type is permitted. When calling a generic function, Go can often use type inference to determine the intended type without the caller needing to specify it explicitly.
2. Declaring Generic Types
Similar to functions, structs and other types can be made generic by adding a type parameter list after the type name. These type parameters can then be used anywhere a regular type would be placed within the struct fields.
Syntax Example: Generic Struct
1 | // A generic linked list node |
When creating an instance of a generic type, you must provide the specific type arguments in square brackets, such as &Deck[string]{} or Node[int]{}.
3. Defining Methods on Generic Types
When defining a method for a generic type, the receiver must include the type parameters defined in the base type declaration. These parameters act as placeholders within the method body.
Syntax Example: Generic Method
1 | // The receiver (d *Deck[C]) includes the type parameter C |
Important Limitation: In Go, methods cannot have their own unique type parameters that aren’t already defined on the receiver type. If a function requires additional type parameters, it must be declared as a top-level function instead of a method.
4. Type Constraints and Interfaces
Constraints act as “types of types,” limiting the set of types that can satisfy a parameter. Go 1.18 enhanced interface syntax to support these constraints:
- Union Types: The pipe symbol
|allows a parameter to be one of several types (e.g.,int | int64 | float64). - The Tilde
~Operator: This indicates that the constraint includes all types whose underlying type matches the specified type. - The
comparableConstraint: A special predeclared interface that limits types to those that support the==and!=operators.
Code Example: Complex Constraints
1 | // Custom constraint for numeric types |
Conclusion
The syntax of Go generics is designed to balance power and readability while maintaining the language’s core principles of simplicity. By shifting the complexity of handling multiple types from the user of a library to the developer of the library, Go ensures that the resulting code remains safe and efficient.
Analogy for Understanding: Think of a generic type like a labeled shipping container. The container itself has a standard structure (the struct), but the “label” (the type parameter) specifies exactly what kind of cargo it is allowed to carry—whether that be electronics, produce, or liquid. The constraint acts like the shipping regulations: it ensures you don’t try to pump liquid into a container designed only for solid crates.
More
Recent Articles:
- Why Go Generics? An Introduction to Parametric Polymorphism on Website
- Rob Pike’s Reaction to GenAI: The Collision of Simplicity and Complexity on Medium on Website
- From Dice-Rolling Probabilities to Cognitive Interfaces: How Generative AI Evolved on Medium on Website
Random Article:
Last Year’s Article (Today):
More Series Articles about You Should Know In Golang:
https://wesley-wei.medium.com/list/you-should-know-in-golang-e9491363cd9a
And I’m Wesley, delighted to share knowledge from the world of programming.
Don’t forget to follow me for more informative content, or feel free to share this with others who may also find it beneficial. It would be a great help to me.
Give me some free applauds, highlights, or replies, and I’ll pay attention to those reactions, which will determine whether I continue to post this type of article.
See you in the next article. 👋
中文文章: https://programmerscareer.com/zh-cn/go-generics-02/
Author: Medium,LinkedIn,Twitter
Note: Originally written at https://programmerscareer.com/go-generics-02/ at 2026-01-03 11:57.
Copyright: BY-NC-ND 3.0
Comments