Skip to content

Commit

Permalink
Merge pull request adambard#662 from vvo/patch-1
Browse files Browse the repository at this point in the history
fix(go func factory): fix func factory example
  • Loading branch information
levibostian committed Jul 2, 2014
2 parents f90cdc4 + 74c0a49 commit 5bd6cb3
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions go.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -192,16 +192,26 @@ func learnFlowControl() {
goto love
love:

learnFunctionFactory() // func returning func is fun(3)(3)
learnDefer() // A quick detour to an important keyword.
learnInterfaces() // Good stuff coming up!
}

func learnFunctionFactory() {
// Next two are equivalent, with second being more practical
fmt.Println(sentenceFactory("summer")("A beautiful", "day!"))

d := sentenceFactory("summer")
fmt.Println(d("A beautiful", "day!"))
fmt.Println(d("A lazy", "afternoon!"))
}

// Decorators are common in other languages. Same can be done in Go
// with function literals that accept arguments.
func learnFunctionFactory(mystring string) func(before, after string) string {
return func(before, after string) string {
return fmt.Sprintf("%s %s %s", before, mystring, after) // new string
}
func sentenceFactory(mystring string) func(before, after string) string {
return func(before, after string) string {
return fmt.Sprintf("%s %s %s", before, mystring, after) // new string
}
}

// Next two are equivalent, with second being more practical
Expand Down

0 comments on commit 5bd6cb3

Please sign in to comment.