FAQ
Is there anyway within Golang to access closed over variables? Couldn't
workout/find a way to do using reflect

eg in:

func main() {
a := makefunc(3)
s := makefunc(44)

fmt.Println("a", &a, a, a())
fmt.Println("s", &s, s, s()) // 1. can get the pointer to the value, how
about the closed over values ?
}

//very simple closure
func makefunc(x int) func() int {
return func() int {
x++
return x
}
}

I'd like to do something like

for i:=0; i++; i<reflect.ValueOf(s).NumClosureVariables() {
        reflect.ValueOf(s).ClosureVariables[i]
}

akin to http://golang.org/pkg/reflect/#Value.Method

This seems impossible within Go without pointer arithmetic.. Is there a
tool which will look this deep?

--
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/groups/opt_out.

Search Discussions

  • Egon at Dec 7, 2013 at 7:59 pm
    I don't think it's possible to access the variables, but you can make the
    variables explicit struct and define a function for them.

    http://play.golang.org/p/_kOIc_CvVH

    + egon
    On Saturday, December 7, 2013 7:58:43 PM UTC+2, Xu En wrote:

    Is there anyway within Golang to access closed over variables? Couldn't
    workout/find a way to do using reflect

    eg in:

    func main() {
    a := makefunc(3)
    s := makefunc(44)

    fmt.Println("a", &a, a, a())
    fmt.Println("s", &s, s, s()) // 1. can get the pointer to the value, how
    about the closed over values ?
    }

    //very simple closure
    func makefunc(x int) func() int {
    return func() int {
    x++
    return x
    }
    }

    I'd like to do something like

    for i:=0; i++; i<reflect.ValueOf(s).NumClosureVariables() {
    reflect.ValueOf(s).ClosureVariables[i]
    }

    akin to http://golang.org/pkg/reflect/#Value.Method

    This seems impossible within Go without pointer arithmetic.. Is there a
    tool which will look this deep?
    --
    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/groups/opt_out.
  • Jsor at Dec 8, 2013 at 10:31 am
    Just return the pointer to the variables you want to get/set the values of:

    http://play.golang.org/p/6Q-0a3o4OX

    (Of course, as always, keep in mind the standard disclaimers about
    concurrency-safety and such where applicable).
    On Saturday, December 7, 2013 10:58:43 AM UTC-7, Xu En wrote:

    Is there anyway within Golang to access closed over variables? Couldn't
    workout/find a way to do using reflect

    eg in:

    func main() {
    a := makefunc(3)
    s := makefunc(44)

    fmt.Println("a", &a, a, a())
    fmt.Println("s", &s, s, s()) // 1. can get the pointer to the value, how
    about the closed over values ?
    }

    //very simple closure
    func makefunc(x int) func() int {
    return func() int {
    x++
    return x
    }
    }

    I'd like to do something like

    for i:=0; i++; i<reflect.ValueOf(s).NumClosureVariables() {
    reflect.ValueOf(s).ClosureVariables[i]
    }

    akin to http://golang.org/pkg/reflect/#Value.Method

    This seems impossible within Go without pointer arithmetic.. Is there a
    tool which will look this deep?
    --
    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/groups/opt_out.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedDec 7, '13 at 5:58p
activeDec 8, '13 at 10:31a
posts3
users3
websitegolang.org

3 users in discussion

Xu En: 1 post Egon: 1 post Jsor: 1 post

People

Translate

site design / logo © 2021 Grokbase