Using Generics (Go 1.18+)

Generics in Go allow functions and data structures to be written with type parameters.

func Sum[T int | float64](a, b T) T {
  return a + b
}

Use square brackets [] to define and use type parameters. Generics improve code reusability and type safety.

← PrevNext →