FAQ
Hello!
I am trying examples from gobook
<http://www.golang-book.com/7/index.htm#section4> (closures).
That's my program:
package main

import "fmt"

func main() {
     nextEven := makeEvenGenerator()
     fmt.Println(nextEven())
     fmt.Println(plus2(3))
     fmt.Println(nextEven())
     fmt.Println(plus2(3))
     fmt.Println(nextEven())
}

func makeEvenGenerator() func() uint {
     i := uint(0)
     return func() (ret uint) {
         ret = i
         i += 2
         return
     }
}

func plus2 (x int) func() int {
     y := int(1)
     return func() (ret int) {
         y += 1
         ret = y + x
         return
     }
}

But something runs wrong!
Output:

0x8048f60
2
0x8048f60
2

Thanks for help!
Sorry for my bad English.

Alex

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Search Discussions

  • Jesse McNelis at Jul 17, 2014 at 3:29 am

    On Thu, Jul 17, 2014 at 6:40 AM, wrote:
    fmt.Println(plus2(3))
    func plus2 (x int) func() int {
    y := int(1)
    return func() (ret int) {
    y += 1
    ret = y + x
    return
    }
    }
    plus2 returns a function, but you never call it, you're just printing
    the address of the function.
    You probably want something like:
    http://play.golang.org/p/FyuagQVjg4

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Zizon Qiu at Jul 17, 2014 at 4:27 am
    http://play.golang.org/p/VN-z9I_fx9 prints

    0x20580
    2
    0x20580
    4
    which the last is 4 not 2, as expected.

    are you sure it did print 2 with above code in your environment?


    On Thu, Jul 17, 2014 at 4:40 AM, wrote:

    Hello!
    I am trying examples from gobook
    <http://www.golang-book.com/7/index.htm#section4> (closures).
    That's my program:
    package main

    import "fmt"

    func main() {
    nextEven := makeEvenGenerator()
    fmt.Println(nextEven())
    fmt.Println(plus2(3))
    fmt.Println(nextEven())
    fmt.Println(plus2(3))
    fmt.Println(nextEven())
    }

    func makeEvenGenerator() func() uint {
    i := uint(0)
    return func() (ret uint) {
    ret = i
    i += 2
    return
    }
    }

    func plus2 (x int) func() int {
    y := int(1)
    return func() (ret int) {
    y += 1
    ret = y + x
    return
    }
    }

    But something runs wrong!
    Output:

    0x8048f60
    2
    0x8048f60
    2

    Thanks for help!
    Sorry for my bad English.

    Alex

    --
    You received this message because you are subscribed to the Google Groups
    "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an
    email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedJul 17, '14 at 3:10a
activeJul 17, '14 at 4:27a
posts3
users3
websitegolang.org

People

Translate

site design / logo © 2021 Grokbase