// comparable represents types that can be compared.typecomparableinterface{int|float64|string}// Max returns the maximum of two comparable values.funcMax[Tcomparable](a,bT)T{ifa>b{returna}returnb}funcmain(){// Find the maximum of two integers.maxInt:=Max(10,20)fmt.Println("Max integer:",maxInt)// Find the maximum of two floats.maxFloat:=Max(3.14,2.71)fmt.Println("Max float:",maxFloat)// Find the maximum of two strings.maxString:=Max("apple","banana")fmt.Println("Max string:",maxString)}
example 2
// Pair[T, U] represents a generic pair of values.typePair[T,Uany]struct{FirstTSecondU}funcmain(){pair:=Pair[int,string]{First:42,Second:"hello"}fmt.Println("First:",pair.First)fmt.Println("Second:",pair.Second)// Print the types of the values in the pair.fmt.Println("Type of First:",reflect.TypeOf(pair.First))fmt.Println("Type of Second:",reflect.TypeOf(pair.Second))}