FAQ
This is probably an easy question, but I can't figure out how to get this
to work.

I need to append a value to a slice of an unknown type. The slice is
passed into a function as an empty interface, and I can use the reflect
package to deal with it. However I can't figure out how to get the value
I'm working to be the pointer of the slice as opposed to copying it.

See this example:

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


I've tried passing in the slice's pointer, but that doesn't work. I can't
get the appended value back out to the original slice.

I've tried copying the val.Interface() back into the slice at the end of
the function, but that doesn't work as well.

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


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

  • Steven Blenkinsop at Oct 19, 2013 at 4:28 pm
    Your interface parameter contains a copy of the value passed to it, so
    reassigning it has no impact on the caller, regardless of whether you put a
    pointer in it or not.

    You want to set the referand of the pointer in the interface. You can do
    this using Value.Set:

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

    On Saturday, 19 October 2013, Tim Shannon wrote:

    This is probably an easy question, but I can't figure out how to get this
    to work.

    I need to append a value to a slice of an unknown type. The slice is
    passed into a function as an empty interface, and I can use the reflect
    package to deal with it. However I can't figure out how to get the value
    I'm working to be the pointer of the slice as opposed to copying it.

    See this example:

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


    I've tried passing in the slice's pointer, but that doesn't work. I can't
    get the appended value back out to the original slice.

    I've tried copying the val.Interface() back into the slice at the end of
    the function, but that doesn't work as well.

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


    --
    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.
    --
    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.
  • Steven Blenkinsop at Oct 19, 2013 at 4:31 pm
    Whoops, posting from my phone. Forgot to remove the now-useless Indirect
    call:
    http://play.golang.org/p/xIQOHXSV5J
    On Saturday, 19 October 2013, Steven Blenkinsop wrote:

    Your interface parameter contains a copy of the value passed to it, so
    reassigning it has no impact on the caller, regardless of whether you put a
    pointer in it or not.

    You want to set the referand of the pointer in the interface. You can do
    this using Value.Set:

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

    On Saturday, 19 October 2013, Tim Shannon wrote:

    This is probably an easy question, but I can't figure out how to get this
    to work.

    I need to append a value to a slice of an unknown type. The slice is
    passed into a function as an empty interface, and I can use the reflect
    package to deal with it. However I can't figure out how to get the value
    I'm working to be the pointer of the slice as opposed to copying it.

    See this example:

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


    I've tried passing in the slice's pointer, but that doesn't work. I
    can't get the appended value back out to the original slice.

    I've tried copying the val.Interface() back into the slice at the end of
    the function, but that doesn't work as well.

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


    --
    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.
    --
    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
postedOct 19, '13 at 3:28p
activeOct 19, '13 at 4:31p
posts3
users2
websitegolang.org

2 users in discussion

Steven Blenkinsop: 2 posts Tim Shannon: 1 post

People

Translate

site design / logo © 2022 Grokbase