Skip to content

Instantly share code, notes, and snippets.

@tevin-morake
Created April 8, 2019 12:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tevin-morake/dfe3c7a90df53b5164e3d6f77a64ad95 to your computer and use it in GitHub Desktop.
Save tevin-morake/dfe3c7a90df53b5164e3d6f77a64ad95 to your computer and use it in GitHub Desktop.
Sometimes we need a simple way to reduce values in go, but we know that go isn't JS. This gist aims at mimicking the js reduce method
package main
import (
"fmt"
)
// This is my way of reducing in go
func main() {
reportMarks := []int{20, 45, 78, 3, 59, 95, 6}
totalMarks := sum(reportMarks...)
fmt.Printf("Among the three classes, the top tally for the school exam is : %d", totalMarks)
}
func sum(marks ...int) int {
total := 0
for _, v := range marks {
total += v
}
return total
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment