Skip to content

Instantly share code, notes, and snippets.

@ajkavanagh
Created August 20, 2017 07:30
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 ajkavanagh/81a8d0140cb2cfa463d7b76b84e8f896 to your computer and use it in GitHub Desktop.
Save ajkavanagh/81a8d0140cb2cfa463d7b76b84e8f896 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
)
// Accumulate takes a []string, applies func f to each
// element and returns the resulting []string.
func Accumulate(s []string, f func(string) string) []string {
for i := range s {
s[i] = f(s[i])
}
return s
}
func change(x string) string {
x = x + "one"
return x
}
func main() {
a := []string{"Hello", "there"}
fmt.Printf("Strings: %s\n", a)
b := Accumulate(a, change)
fmt.Printf("a strings: %s\n", a)
fmt.Printf("b strings: %s\n", b)
}
Strings: [Hello there]
a strings: [Helloone thereone]
b strings: [Helloone thereone]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment