| 1 | package main |
| 2 | |
| 3 | type Number interface { |
| 4 | int | int64 | ~float64 |
| 5 | } |
| 6 | |
| 7 | func Sum[T Number](numbers []T) T { |
| 8 | var total T |
| 9 | for _, x := range numbers { |
| 10 | total += x |
| 11 | } |
| 12 | return total |
| 13 | } |
| 14 | |
| 15 | func main() { |
| 16 | xs := []int{3, 5, 10} |
| 17 | total := Sum(xs) |
| 18 | println(total) |
| 19 | } |
| 20 | |
| 21 | // Output: |
| 22 | // 18 |