← All Exercises

Go Generic Constraints

advancedgo~25min

Background

Go generic constraints enable expressing type requirements like ordering, numeric operations, or custom interfaces.

Problem

Write a generic Min and Max function using a custom Ordered constraint that works with any comparable ordered type.

Input

Two values of any ordered type (int, float, string).

Output

The minimum and maximum of the two values.

Sample tests

Sample 1

Input: Min(3, 7)
Expected: 3

Your solution

Learning objective

Learn generic type constraints, union type elements, and the tilde operator for underlying types.