Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(go func factory): fix func factory example #662

Merged
merged 1 commit into from
Jul 2, 2014
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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