Go Generics
Generics are an important feature introduced in Go 1.18, enabling developers to write more flexible and reusable code.
Generics are implemented mainly through the following two core concepts:
- **Type Parameters:** allow you to use one or more types as parameters in a function or type definition.
- **Type Constraints:** specify the conditions that type parameters must satisfy, ensuring these types can be safely operated on within the function.
| Concept | Purpose | Example |
|---|---|---|
| Type parameters | Declared after the function or type name, representing a type to be determined. | [T any] |
| Type Constraints | Defines the conditions that type parameters must satisfy (such as supported operators or methods). | int,float64,comparable,constraints.Ordered,any |
any |
Constrains type parameters toanytype. | [T any] |
comparable |
Constrains type parameters tocomparable's type. | [K comparable] |
Generics allow us to write code that does not depend on a specific data type.
Before generics were introduced, if we wanted to handle data of different types, we usually had to write duplicate functions for each type.
Limitations of the traditional approach:
Example
// A function that handles the int type
func
MaxInt
(
a
,
b
int
)
int
{
if
a > b
{
return
a
}
return
b
}
// A function that handles the float64 type
func
MaxFloat
(
a
,
b
float64
)
float64
{
if
a > b
{
return
a
}
return
b
}
A solution using generics:
Example
// One function handles multiple types
func
Max
[
T comparable
](
a
,
b T
)
T
{
if
a > b
{
return
a
}
return
b
}
Generics syntax explained
Type parameter declarations
Generic functions and types are declared through a type parameter list, with the syntax [类型参数 约束]。
Example
// Basic syntax structure
func
Function name
[
T constraint
](
Parameter T
)
Return value type
{
// Function body
}
type
Type name
[
T constraint
]
struct
{
// Struct fields
}
Type parameter naming conventions
- Uppercase letters are typically used:
T、K、V、Eetc. T: stands for Type (type)K: stands for Key (key)V: stands for Value (value)E: stands for Element (element)
Constraints
A constraint defines the conditions that a type parameter must satisfy; it is a core concept of generics.
Built-in constraints
1. any Constraint
any is the empty interface interface{} an alias of it, meaning any type is allowed.
Example
func
PrintAny
[
T any
](
value T
)
{
fmt
.
Printf
(
"Value: %v, Type: %T\n"
,
value
,
value
)
}
// Usage example
PrintAny
(
42
)
// Value: 42, Type: int
PrintAny
(
"hello"
)
// Value: hello, Type: string
PrintAny
(
3
.
14
)
// Value: 3.14, Type: float64
2. comparable Constraint
comparable indicates that the type supports == and != operator.
Example
func
FindIndex
[
T comparable
](
slice
[]
T
,
target T
)
int
{
for
i
,
v
:=
range
slice
{
if
v
==
target
{
return
i
}
}
return
1
}
// Usage example
numbers
:=
[]
int
{
1
,
2
,
3
,
4
,
5
}
fmt
.
Println
(
FindIndex
(
numbers
,
3
))
// Output: 2
names
:=
[]
string
{
"Alice"
,
"Bob"
,
"Charlie"
}
fmt
.
Println
(
FindIndex
(
names
,
"Bob"
))
// Output: 1
3. Union Constraints
Use | The operator combines multiple types.
Example
// Numeric type constraint
type
Number
interface
{
int
|
int8
|
int16
|
int32
|
int64
|
uint
|
uint8
|
uint16
|
uint32
|
uint64
|
float32
|
float64
}
func
Add
[
T Number
](
a
,
b T
)
T
{
return
a
b
}
// Usage example
fmt
.
Println
(
Add
(
10
,
20
))
// Output: 30
fmt
.
Println
(
Add
(
3
.
14
,
2
.
71
))
// Output: 5.85
Custom constraints
1. Method constraints
Define constraints that require specific methods.
Example
// Define the Stringer constraint
type
Stringer
interface
{
String
()
string
}
func
PrintString
[
T Stringer
](
value T
)
{
fmt
.
Println
(
value
.
String
())
}
// Implement a custom type
type
Person
struct
{
Name
string
Age
int
}
func
(
p Person
)
String
()
string
{
return
fmt
.
Sprintf
(
"%s (%d years old)"
,
p
.
Name
,
p
.
Age
)
}
// Usage example
person
:=
Person
{
Name
:
"Alice"
,
Age
:
25
}
PrintString
(
person
)
// Output: Alice (25 years old)
2. Complex constraints
Combines type and method requirements.
Example
// Require the type to be numeric and implement the String() method
type
NumericStringer
interface
{
Number
String
()
string
}
Generic function practice
1. General utility functions
Example
// Swap two values
func
Swap
[
T any
](
a
,
b T
)
(
T
,
T
)
{
return
b
,
a
}
// Check whether a slice contains an element
func
Contains
[
T comparable
](
slice
[]
T
,
target T
)
bool
{
for
_
,
item
:=
range
slice
{
if
item
==
target
{
return
true
}
}
return
false
}
// Deduplication function
func
Unique
[
T comparable
](
slice
[]
T
)
[]
T
{
seen
:=
make
(
map
[
T
]
bool
)
result
:=
[]
T
{}
for
_
,
item
:=
range
slice
{
if
!
seen
[
item
]
{
seen
[
item
]
=
true
result
=
append
(
result
,
item
)
}
}
return
result
}
// Usage example
func
main
()
{
// Swap example
a
,
b
:=
10
,
20
a
,
b
=
Swap
(
a
,
b
)
fmt
.
Printf
(
"a=%d, b=%d\n"
,
a
,
b
)
// Output: a=20, b=10
// Contains example
numbers
:=
[]
int
{
1
,
2
,
3
,
4
,
5
}
fmt
.
Println
(
Contains
(
numbers
,
3
))
// Output: true
// Unique example
duplicates
:=
[]
int
{
1
,
2
,
2
,
3
,
4
,
4
,
5
}
unique
:=
Unique
(
duplicates
)
fmt
.
Println
(
unique
)
// Output: [1 2 3 4 5]
}
2. Mathematical operation functions
Example
// Find the maximum value in a slice
func
Max
[
T Number
](
slice
[]
T
)
T
{
if
len
(
slice
)
==
0
{
var
zero T
return
zero
}
max
:=
slice
[
0
]
for
_
,
value
:=
range
slice
[
1
:]
{
if
value > max
{
max
=
value
}
}
return
max
}
// Find the minimum value in a slice
func
Min
[
T Number
](
slice
[]
T
)
T
{
if
len
(
slice
)
==
0
{
var
zero T
return
zero
}
min
:=
slice
[
0
]
for
_
,
value
:=
range
slice
[
1
:]
{
if
value < min
{
min
=
value
}
}
return
min
}
// Find the average value of a slice
func
Average
[
T Number
](
slice
[]
T
)
float64
{
if
len
(
slice
)
==
0
{
return
0
}
var
sum T
for
_
,
value
:=
range
slice
{
sum
+=
value
}
return
float64
(
sum
)
/
float64
(
len
(
slice
))
}
// Usage example
func
main
()
{
ints
:=
[]
int
{
1
,
5
,
3
,
9
,
2
}
floats
:=
[]
float64
{
1
.
1
,
5
.
5
,
3
.
3
,
9
.
9
,
2
.
2
}
fmt
.
Printf
(
"Max int: %d\n"
,
Max
(
ints
))
// Output: 9
fmt
.
Printf
(
"Min float: %.1f\n"
,
Min
(
floats
))
// Output: 1.1
fmt
.
Printf
(
"Average: %.2f\n"
,
Average
(
floats
))
// Output: 4.40
}
Generic type
1. Generic structs
Example
// Generic stack implementation
type
Stack
[
T any
]
struct
{
elements
[]
T
}
// Push
func
(
s
Stack
[
T
])
Push
(
value T
)
{
s
.
elements
=
append
(
s
.
elements
,
value
)
}
// Pop
func
(
s
Stack
[
T
])
Pop
()
(
T
,
bool
)
{
if
len
(
s
.
elements
)
==
0
{
var
zero T
return
zero
,
false
}
lastIndex
:=
len
(
s
.
elements
)
1
value
:=
s
.
elements
[
lastIndex
]
s
.
elements
=
s
.
elements
[:
lastIndex
]
return
value
,
true
}
// Peek at the top element of the stack
func
(
s
Stack
[
T
])
Peek
()
(
T
,
bool
)
{
if
len
(
s
.
elements
)
==
0
{
var
zero T
return
zero
,
false
}
return
s
.
elements
[
len
(
s
.
elements
)
1
],
true
}
// Check whether the stack is empty
func
(
s
Stack
[
T
])
IsEmpty
()
bool
{
return
len
(
s
.
elements
)
==
0
}
// Usage example
func
main
()
{
// Integer stack
intStack
:=
Stack
[
int
]{}
intStack
.
Push
(
1
)
intStack
.
Push
(
2
)
intStack
.
Push
(
3
)
fmt
.
Println
(
intStack
.
Pop
())
// Output: 3 true
// String stack
stringStack
:=
Stack
[
string
]{}
stringStack
.
Push
(
"hello"
)
stringStack
.
Push
(
"world"
)
fmt
.
Println
(
stringStack
.
Pop
())
// Output: world true
}
2. Generic maps
Example
// Thread-safe generic map
type
SafeMap
[
K comparable
,
V any
]
struct
{
data
map
[
K
]
V
mutex
sync.RWMutex
}
// Create a new SafeMap
func
NewSafeMap
[
K comparable
,
V any
]()
SafeMap
[
K
,
V
]
{
return
&SafeMap
[
K
,
V
]{
data
:
make
(
map
[
K
]
V
),
}
}
// Set key-value pairs
func
(
m
SafeMap
[
K
,
V
])
Set
(
key K
,
value V
)
{
m
.
mutex
.
Lock
()
defer
m
.
mutex
.
Unlock
()
m
.
data
[
key
]
=
value
}
// Get the value
func
(
m
SafeMap
[
K
,
V
])
Get
(
key K
)
(
V
,
bool
)
{
m
.
mutex
.
RLock
()
defer
m
.
mutex
.
RUnlock
()
value
,
exists
:=
m
.
data
[
key
]
return
value
,
exists
}
// Delete a key
func
(
m
SafeMap
[
K
,
V
])
Delete
(
key K
)
{
m
.
mutex
.
Lock
()
defer
m
.
mutex
.
Unlock
()
delete
(
m
.
data
,
key
)
}
// Get all keys
func
(
m
SafeMap
[
K
,
V
])
Keys
()
[]
K
{
m
.
mutex
.
RLock
()
defer
m
.
mutex
.
RUnlock
()
keys
:=
make
([]
K
,
0
,
len
(
m
.
data
))
for
key
:=
range
m
.
data
{
keys
=
append
(
keys
,
key
)
}
return
keys
}
// Usage example
func
main
()
{
// Create a map from string to int
scores
:=
NewSafeMap
[
string
,
int
]()
scores
.
Set
(
"Alice"
,
95
)
scores
.
Set
(
"Bob"
,
87
)
if
score
,
exists
:=
scores
.
Get
(
"Alice"
);
exists
{
fmt
.
Printf
(
"Alice's score: %d\n"
,
score
)
// Output: Alice's score: 95
}
fmt
.
Println
(
"Keys:"
,
scores
.
Keys
())
// Output: Keys: [Alice Bob]
}
Type Inference
The Go compiler can automatically infer type parameters, making the code more concise.
Example
// No need to specify types explicitly
func
main
()
{
// Type inference example
fmt
.
Println
(
Max
([]
int
{
1
,
2
,
3
}))
// The compiler infers T as int
fmt
.
Println
(
Max
([]
float64
{
1
.
1
,
2
.
2
}))
// The compiler infers T as float64
// Specify the type explicitly (sometimes necessary)
var
result
int
=
Max
[
int
]([]
int
{
1
,
2
,
3
})
fmt
.
Println
(
result
)
}
Practical Exercise
Exercise 1: Implement a generic filter
Write a Filter function that filters slice elements based on a condition.
Example
// Your implementation code goes here
func
Filter
[
T any
](
slice
[]
T
,
predicate
func
(
T
)
bool
)
[]
T
{
// Implement the filtering logic
}
// Test code
func
main
()
{
numbers
:=
[]
int
{
1
,
2
,
3
,
4
,
5
,
6
}
even
:=
Filter
(
numbers
,
func
(
n
int
)
bool
{
return
n
%
2
==
0
})
fmt
.
Println
(
even
)
// Should output: [2 4 6]
}
Exercise 2: Implement a generic map function
Write a Map function that converts each element in a slice to another type.
Example
// Your implementation code goes here
func
Map
[
T any
,
U any
](
slice
[]
T
,
mapper
func
(
T
)
U
)
[]
U
{
// Implement the mapping logic
}
// Test code
func
main
()
{
numbers
:=
[]
int
{
1
,
2
,
3
,
4
,
5
}
strings
:=
Map
(
numbers
,
func
(
n
int
)
string
{
return
fmt
.
Sprintf
(
"Number: %d"
,
n
)
})
fmt
.
Println
(
strings
)
}
Common Issues and Notes
1. Performance Considerations
Generics perform type specialization at compile time, and runtime performance is comparable to hand-written code for specific types.
2. Choosing type constraints
- Use
anyis the most flexible, but limited in functionality - Use
comparablesupports equality comparison - Use union constraints to restrict the available concrete types
3. Error Handling
Example
// Good error-handling practices
func
SafeMax
[
T Number
](
slice
[]
T
)
(
T
,
error
)
{
if
len
(
slice
)
==
0
{
var
zero T
return
zero
,
errors
.
New
(
"slice is empty"
)
}
return
Max
(
slice
),
nil
}