golang closure variable scope
Posted
by
waaadim
on Stack Overflow
See other posts from Stack Overflow
or by waaadim
Published on 2013-11-13T15:12:28Z
Indexed on
2013/11/13
15:54 UTC
Read the original article
Hit count: 299
I'm reading 'CreateSpace An Introduction to Programming in Go 2012'
and on page 86 I found this evil magic
func makeEvenGenerator() func() uint {
i := uint(0)
return func() (ret uint) {
ret = i
i += 2
return
}
}
// here's how it's called
nextEven := makeEvenGenerator()
fmt.Println(nextEven())
fmt.Println(nextEven())
fmt.Println(nextEven())
1) Why is i
not resetting ?
2) is nextEven()
returning and uint
or is Println
so smart that it can work with everything ?
© Stack Overflow or respective owner