summaryrefslogtreecommitdiffhomepage
path: root/go.html.markdown
diff options
context:
space:
mode:
authorLevi Bostian <levi.bostian@gmail.com>2014-07-02 08:56:13 -0500
committerLevi Bostian <levi.bostian@gmail.com>2014-07-02 08:56:13 -0500
commit5bd6cb3eb848c94ce31b4d1233f72bd576bb9cb7 (patch)
treed07ea17deeba08f7ef24aa5e875c8635abc072d7 /go.html.markdown
parentf90cdc44bb98ec4d275dd21c321adfb411d3e9c1 (diff)
parent74c0a49e6d79e80b1f0a511afe3eece0cde6f43c (diff)
Merge pull request #662 from vvo/patch-1
fix(go func factory): fix func factory example
Diffstat (limited to 'go.html.markdown')
-rw-r--r--go.html.markdown18
1 files changed, 14 insertions, 4 deletions
diff --git a/go.html.markdown b/go.html.markdown
index a9a7de72..980fdc66 100644
--- a/go.html.markdown
+++ b/go.html.markdown
@@ -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