Start of change

Using generics to represent functions and types in a generic form

You can use generics to declare and use functions or types that are written to work with any of a set of types provided by calling code. The type parameters to type and function declarations are optional when using generics function.

To add generics function, you must have a module to hold your code and specify a module path that's specific to your own needs. The generics function supports value of different types, which needs a declaration of type parameters in addition to its ordinary function parameters. Each type parameter has a type constraint that acts as the meta-type for the type parameter. Each type constraint specifies the permissible type arguments that calling code can use for the respective type parameter.

Though a type parameter's constraint typically represents a set of types, the type parameter stands for a single type – the type provided as a type argument by the calling code at compile time. If the type argument's type is not allowed by the type parameter's constraint, then the code won't compile.
Note:
  1. A type must support all the operations the generic code is performing on it. For example, if your function's code is to try to perform string operations (such as indexing) on a type parameter whose constraint included numeric types, the code will not compile.
  2. You can omit type arguments in calling code when the Go compiler can infer the types you want to use. The compiler infers type arguments from the types of function arguments. If you need to call a generic function that has no arguments, you will need to include the type arguments in the function call.

Example

In this example, there is a function MapSlice which takes a slice of any type, and produces a map that maps the indices of the slice to the value contained at that index.
func MapSlice[V any](values []V) map[int]V {
	mymap := make(map[int]V)
	for i, v := range values {
		mymap[i] = v
	}
	return mymap
}
End of change