Created
April 8, 2019 12:00
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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