From 55e1c2adafcf0f2590633064aaf83af5ff1964af Mon Sep 17 00:00:00 2001 From: Sam Zaydel Date: Wed, 9 Jul 2014 20:56:01 -0700 Subject: Go inline function literals as arguments to other functions or function literals. --- go.html.markdown | 9 +++++++++ 1 file changed, 9 insertions(+) (limited to 'go.html.markdown') diff --git a/go.html.markdown b/go.html.markdown index a9a7de72..e496117e 100644 --- a/go.html.markdown +++ b/go.html.markdown @@ -188,6 +188,15 @@ func learnFlowControl() { x = 1.3e3 // This makes x == 1300 fmt.Println("xBig:", xBig()) // false now. + // What's more is function literals may be defined and called inline, + // acting as an argument to function, as long as: + // a) function literal is called immediately (), + // b) result type matches expected type of argument. + fmt.Println("Add + double two numbers: ", + func(a, b int) int { + return (a + b) * 2 + }(10, 2)) // Called with args (10, 2) + // When you need it, you'll love it. goto love love: -- cgit v1.2.3 From 174f0baa14421834e181f37bd969d7f015a02adc Mon Sep 17 00:00:00 2001 From: Sam Zaydel Date: Thu, 10 Jul 2014 09:22:47 -0700 Subject: Small change to inline literal functions comments. --- go.html.markdown | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'go.html.markdown') diff --git a/go.html.markdown b/go.html.markdown index e496117e..a5ea5859 100644 --- a/go.html.markdown +++ b/go.html.markdown @@ -195,7 +195,8 @@ func learnFlowControl() { fmt.Println("Add + double two numbers: ", func(a, b int) int { return (a + b) * 2 - }(10, 2)) // Called with args (10, 2) + }(10, 2)) // Called with args 10 and 2 + // Add + double two numbers: 24 // When you need it, you'll love it. goto love -- cgit v1.2.3 From 7cddab2925260946c61eac60cc62764017a8e8af Mon Sep 17 00:00:00 2001 From: Sam Zaydel Date: Thu, 10 Jul 2014 09:25:48 -0700 Subject: Forgot to add `=>` to comment line. --- go.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'go.html.markdown') diff --git a/go.html.markdown b/go.html.markdown index a5ea5859..88e2c8da 100644 --- a/go.html.markdown +++ b/go.html.markdown @@ -196,7 +196,7 @@ func learnFlowControl() { func(a, b int) int { return (a + b) * 2 }(10, 2)) // Called with args 10 and 2 - // Add + double two numbers: 24 + // => Add + double two numbers: 24 // When you need it, you'll love it. goto love -- cgit v1.2.3