main.go 244 B

1234567891011121314151617181920
  1. package main
  2. /* Go demonstration of generics. */
  3. import (
  4. "fmt"
  5. )
  6. func Sum[V int | int64 | float64 | string](a []V) V {
  7. var s V
  8. for _, v := range a {
  9. s += v
  10. }
  11. return s
  12. }
  13. func main() {
  14. fmt.Println(Sum[int]([]int{1, 2, 3, 4, 10}))
  15. }