FAQ
Is there any way to get the variable name which has assigned a function
literal?

I've played with reflect but I think that it is not possible:

http://play.golang.org/p/Ts-FzCb_mF

--
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

  • Egon at Dec 3, 2014 at 1:32 pm
    Any particular reason you want to do this?
    On Wednesday, 3 December 2014 14:50:11 UTC+2, Archos wrote:

    Is there any way to get the variable name which has assigned a function
    literal?

    I've played with reflect but I think that it is not possible:

    http://play.golang.org/p/Ts-FzCb_mF
    --
    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.
  • Archos at Dec 3, 2014 at 2:05 pm
    From a file file generator, I have a function whose argument is a type of
    function.

    So, at generating the new file, I need to know the name of that type of
    function for that it can be called from the file generated.
    On Wednesday, December 3, 2014 1:31:37 PM UTC, egon wrote:

    Any particular reason you want to do this?
    On Wednesday, 3 December 2014 14:50:11 UTC+2, Archos wrote:

    Is there any way to get the variable name which has assigned a function
    literal?

    I've played with reflect but I think that it is not possible:

    http://play.golang.org/p/Ts-FzCb_mF
    --
    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.
  • Egon at Dec 3, 2014 at 3:04 pm

    On Wednesday, 3 December 2014 16:05:11 UTC+2, Archos wrote:
    From a file file generator, I have a function whose argument is a type of
    function.

    So, at generating the new file, I need to know the name of that type of
    function for that it can be called from the file generated.
    Simply a struct
       http://play.golang.org/p/asx5_YN3c4

    Or wrap/register the funcs with the name
       http://play.golang.org/p/QDKk6EBAeV

    Of course there's a hackish way... but every time you use it, a kitten dies:
    http://play.golang.org/p/3Sx_dPJXkL

    (Not sure whether you meant the function name that's stored in the variable
    name... or actually the variable name - you can't get the variable name.)

    + Egon

    On Wednesday, December 3, 2014 1:31:37 PM UTC, egon wrote:

    Any particular reason you want to do this?
    On Wednesday, 3 December 2014 14:50:11 UTC+2, Archos wrote:

    Is there any way to get the variable name which has assigned a function
    literal?

    I've played with reflect but I think that it is not possible:

    http://play.golang.org/p/Ts-FzCb_mF
    --
    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.
  • Archos at Dec 3, 2014 at 4:25 pm
    Finally, I implemented using a struct/interface, Thanks for the examples.
    On Wednesday, December 3, 2014 3:04:23 PM UTC, egon wrote:
    On Wednesday, 3 December 2014 16:05:11 UTC+2, Archos wrote:

    From a file file generator, I have a function whose argument is a type of
    function.

    So, at generating the new file, I need to know the name of that type of
    function for that it can be called from the file generated.
    Simply a struct
    http://play.golang.org/p/asx5_YN3c4

    Or wrap/register the funcs with the name
    http://play.golang.org/p/QDKk6EBAeV

    Of course there's a hackish way... but every time you use it, a kitten
    dies:
    http://play.golang.org/p/3Sx_dPJXkL

    (Not sure whether you meant the function name that's stored in the
    variable name... or actually the variable name - you can't get the variable
    name.)
    I mean the variable name which calls to the closure.

    + Egon

    On Wednesday, December 3, 2014 1:31:37 PM UTC, egon wrote:

    Any particular reason you want to do this?
    On Wednesday, 3 December 2014 14:50:11 UTC+2, Archos wrote:

    Is there any way to get the variable name which has assigned a function
    literal?

    I've played with reflect but I think that it is not possible:

    http://play.golang.org/p/Ts-FzCb_mF
    --
    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.
  • Dmitri Shuralyov at Dec 4, 2014 at 5:09 am
    If you have access to the source code in its original location when running
    code, you could use this code get the variable name. It uses runtime stack
    trace to figure out the path and line number of the caller, then parses the
    original source AST to find out the variable name. It won't work if the
    source code is missing or moved to a different location.

    http://godoc.org/github.com/shurcooL/go/gists/gist6418290#GetExprAsString
    (better package name suggestions are welcome, I'm not good with coming up
    with names)

    f := func(n int) int {
    return n + 1
    }
    fmt.Println("Name of func:", gist6418290.GetExprAsString(f))

    // Output:
    //f

    Now, it works in practice and was a fun challenge to implement, but I don't
    recommend you actually use it... just because I'm sure there's a simpler
    solution for your particular problem (it looks like you already found it).


    On Wednesday, December 3, 2014 8:25:23 AM UTC-8, Archos wrote:

    Finally, I implemented using a struct/interface, Thanks for the examples.
    On Wednesday, December 3, 2014 3:04:23 PM UTC, egon wrote:
    On Wednesday, 3 December 2014 16:05:11 UTC+2, Archos wrote:

    From a file file generator, I have a function whose argument is a type
    of function.

    So, at generating the new file, I need to know the name of that type of
    function for that it can be called from the file generated.
    Simply a struct
    http://play.golang.org/p/asx5_YN3c4

    Or wrap/register the funcs with the name
    http://play.golang.org/p/QDKk6EBAeV

    Of course there's a hackish way... but every time you use it, a kitten
    dies:
    http://play.golang.org/p/3Sx_dPJXkL

    (Not sure whether you meant the function name that's stored in the
    variable name... or actually the variable name - you can't get the variable
    name.)
    I mean the variable name which calls to the closure.

    + Egon

    On Wednesday, December 3, 2014 1:31:37 PM UTC, egon wrote:

    Any particular reason you want to do this?
    On Wednesday, 3 December 2014 14:50:11 UTC+2, Archos wrote:

    Is there any way to get the variable name which has assigned a
    function literal?

    I've played with reflect but I think that it is not possible:

    http://play.golang.org/p/Ts-FzCb_mF
    --
    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.
  • Dmitri Shuralyov at Dec 4, 2014 at 5:11 am
    The output should've been:

    // Output:
    //Name of func: f

    Sorry, this is so frustrating that there's no way to edit a post I _just_
    made and I'm so used to it everywhere (Gmail, GitHub, HN, reddit, etc.). :(

    On Wednesday, December 3, 2014 9:09:11 PM UTC-8, Dmitri Shuralyov wrote:

    If you have access to the source code in its original location when
    running code, you could use this code get the variable name. It uses
    runtime stack trace to figure out the path and line number of the caller,
    then parses the original source AST to find out the variable name. It won't
    work if the source code is missing or moved to a different location.

    http://godoc.org/github.com/shurcooL/go/gists/gist6418290#GetExprAsString
    (better package name suggestions are welcome, I'm not good with coming up
    with names)

    f := func(n int) int {
    return n + 1
    }
    fmt.Println("Name of func:", gist6418290.GetExprAsString(f))

    // Output:
    //f

    Now, it works in practice and was a fun challenge to implement, but I
    don't recommend you actually use it... just because I'm sure there's a
    simpler solution for your particular problem (it looks like you already
    found it).


    On Wednesday, December 3, 2014 8:25:23 AM UTC-8, Archos wrote:

    Finally, I implemented using a struct/interface, Thanks for the examples.
    On Wednesday, December 3, 2014 3:04:23 PM UTC, egon wrote:
    On Wednesday, 3 December 2014 16:05:11 UTC+2, Archos wrote:

    From a file file generator, I have a function whose argument is a type
    of function.

    So, at generating the new file, I need to know the name of that type of
    function for that it can be called from the file generated.
    Simply a struct
    http://play.golang.org/p/asx5_YN3c4

    Or wrap/register the funcs with the name
    http://play.golang.org/p/QDKk6EBAeV

    Of course there's a hackish way... but every time you use it, a kitten
    dies:
    http://play.golang.org/p/3Sx_dPJXkL

    (Not sure whether you meant the function name that's stored in the
    variable name... or actually the variable name - you can't get the variable
    name.)
    I mean the variable name which calls to the closure.

    + Egon

    On Wednesday, December 3, 2014 1:31:37 PM UTC, egon wrote:

    Any particular reason you want to do this?
    On Wednesday, 3 December 2014 14:50:11 UTC+2, Archos wrote:

    Is there any way to get the variable name which has assigned a
    function literal?

    I've played with reflect but I think that it is not possible:

    http://play.golang.org/p/Ts-FzCb_mF
    --
    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.
  • Lars Seipel at Dec 3, 2014 at 1:35 pm

    On Wed, Dec 03, 2014 at 04:50:11AM -0800, Archos wrote:
    I've played with reflect but I think that it is not possible:
    It's not. At runtime, that name doesn't exist anymore.

    --
    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
postedDec 3, '14 at 12:50p
activeDec 4, '14 at 5:11a
posts8
users4
websitegolang.org

People

Translate

site design / logo © 2021 Grokbase