Linked by kragil on Wed 23rd Jan 2013 20:26 UTC
Thread beginning with comment 550173
To view parent comment, click here.
To read all comments associated with this story, please click here.
To view parent comment, click here.
To read all comments associated with this story, please click here.
RE[6]: Comment by Laurence
by satsujinka on Thu 24th Jan 2013 03:22
in reply to "RE[5]: Comment by Laurence"
I'm not comparing it with JavaScript or C#. I was just showing that it's a 1-to-1 match with how you'd normally write a function that returns a function that computes successive Fibonacci numbers.
Though since you mention it, the JavaScript version looks identical as well.
function fib() {
var a = 0; var b = 1;
return function() {a = b; b = b + 1; return a;};
}
How's pointer or array syntax weird?
func f() []int {...}
func f() *int {...}
There's nothing weird about those signatures.
Array initialization is almost exactly like C:
[]int{1,2,3,4}




Member since:
2005-09-27
I can read the code:
func fib() func() int {
declares a function that returns a function.
a, b := 0, 1
declares a and b as integer then initialized them with a zero.
return func() int {
a, b = b, a+b
return a
}
}
That's the closure, but the difference with its counterparts like Javascript and C# is that you can mix that example with pointers and a weird array initialization syntax that Go allows.
I said Go is a C like language, why do you keep comparing it with Javascript and C#?