Skip to content

Instantly share code, notes, and snippets.

@percybolmer
Last active January 1, 2022 20:37
Show Gist options
  • Save percybolmer/499a2a46317f149cc6912feb520a4115 to your computer and use it in GitHub Desktop.
Save percybolmer/499a2a46317f149cc6912feb520a4115 to your computer and use it in GitHub Desktop.
A summary example without generics
package main
import (
"fmt"
)
func main() {
intvalues := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
int64values := []int64{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
float64values := []float64{1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0}
fmt.Println("Total for ints:", CountTotalInts(intvalues))
fmt.Println("Total for int64:", CountTotalInts64(int64values))
fmt.Println("Total for floats:", CountTotalFloats(float64values))
}
// CountTotalInts64 counts the total for the slice
func CountTotalInts64(points []int64) int64 {
var total int64
for _, v := range points {
total += v
}
return total
}
// CountTotalInts counts the total for the slice
func CountTotalInts(points []int) int {
var total int
for _, v := range points {
total += v
}
return total
}
// CountTotalFloats counts the total for the slice
func CountTotalFloats(points []float64) float64 {
var total float64
for _, v := range points {
total += v
}
return total
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment