FAQ
Hi golang-nuts,

I have a function that takes an interface{}, which must be a pointer to a
slice (i of course check for this). What I want to do is the following:

1) decode a gob encoded []byte (stored on disk) using the element type of
the slice
2) append the new element to the provided slice such that the original
slice grows.

For instance, ignoring the decoding (which I already have working), assume
that all I wanted was to statically append the int values 1 and 2 to a
*[]int. I would hope to get the following:

func main() {
    var ints *[]int

    err := append1And2(ints); err != nil {
       panic(err)
    }

    fmt.Println(*ints) // should be an array of [1,2]

}

I'm providing only the nil value pointer, meaning I need to allocate the
slice in the function. Is there any way of doing this?

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

  • Chris dollin at Jan 30, 2014 at 12:53 pm

    On 30 January 2014 11:26, Patrick-Ranjit D. Madsen wrote:

    Hi golang-nuts,

    I have a function that takes an interface{}, which must be a pointer to a
    slice (i of course check for this). What I want to do is the following:

    1) decode a gob encoded []byte (stored on disk) using the element type of
    the slice
    2) append the new element to the provided slice such that the original
    slice grows.

    For instance, ignoring the decoding (which I already have working), assume
    that all I wanted was to statically append the int values 1 and 2 to a
    *[]int. I would hope to get the following:

    func main() {
    var ints *[]int

    err := append1And2(ints); err != nil {
    panic(err)
    }

    fmt.Println(*ints) // should be an array of [1,2]

    }

    I'm providing only the nil value pointer, meaning I need to allocate the
    slice in the function. Is there any way of doing this?

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


    --
    Chris "allusive" Dollin

    --
    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.
  • Chris dollin at Jan 30, 2014 at 12:57 pm

    On 30 January 2014 11:26, Patrick-Ranjit D. Madsen wrote:

    Hi golang-nuts,

    I have a function that takes an interface{}, which must be a pointer to a
    slice (i of course check for this). What I want to do is the following:

    1) decode a gob encoded []byte (stored on disk) using the element type of
    the slice
    2) append the new element to the provided slice such that the original
    slice grows.

    For instance, ignoring the decoding (which I already have working), assume
    that all I wanted was to statically append the int values 1 and 2 to a
    *[]int. I would hope to get the following:

    func main() {
    var ints *[]int

    err := append1And2(ints); err != nil {
    panic(err)
    }

    fmt.Println(*ints) // should be an array of [1,2]

    }

    I'm providing only the nil value pointer, meaning I need to allocate the
    slice in the function. Is there any way of doing this?
    Append will allocate if necessary. And the first argument to append can
    be (a properly typed) nil.

    Chris

    --
    Chris "allusive" Dollin

    --
    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.
  • Patrick-Ranjit D. Madsen at Jan 30, 2014 at 1:19 pm
    I'm sorry, I should have been more specific. What I'm restoring is arbitrary types, meaning I have to use reflect to determine the type. Pseudo-code:


    func append(out interface{}) error {

      if out == nil {
       return errors.New("Cannot remake from <nil>")
      }

      outv := reflect.ValueOf(out)

      outt := outv.Type()
      outk := outt.Kind()

      if outk != reflect.Ptr {
       return errors.New("StateDB: Can only remake into a pointer to a slice")
      }

      slicet := outt.Elem()
      slicek := slicet.Kind()

      if slicek != reflect.Slice {
       return errors.New("StateDB: Pointer must point to a slice")
      }

      elmt := slicet.Elem()
      elm_ptr := reflect.New(elmt)

      // 1. decode from gobbed []byte - Done!
      // 2. append into the out slice pointer... - Possible?
    }
    On 30 Jan 2014, at 13:57, chris dollin wrote:

    On 30 January 2014 11:26, Patrick-Ranjit D. Madsen wrote:
    Hi golang-nuts,

    I have a function that takes an interface{}, which must be a pointer to a slice (i of course check for this). What I want to do is the following:

    1) decode a gob encoded []byte (stored on disk) using the element type of the slice
    2) append the new element to the provided slice such that the original slice grows.

    For instance, ignoring the decoding (which I already have working), assume that all I wanted was to statically append the int values 1 and 2 to a *[]int. I would hope to get the following:

    func main() {
    var ints *[]int

    err := append1And2(ints); err != nil {
    panic(err)
    }

    fmt.Println(*ints) // should be an array of [1,2]

    }

    I'm providing only the nil value pointer, meaning I need to allocate the slice in the function. Is there any way of doing this?

    Append will allocate if necessary. And the first argument to append can
    be (a properly typed) nil.

    Chris

    --
    Chris "allusive" Dollin
    --
    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.
  • Harald Weidner at Jan 30, 2014 at 1:37 pm
    Hello,
    I have a function that takes an interface{}, which must be a pointer to a
    slice (i of course check for this). What I want to do is the following:
    2) append the new element to the provided slice such that the original
    slice grows.
    For instance, ignoring the decoding (which I already have working), assume
    that all I wanted was to statically append the int values 1 and 2 to a
    *[]int. I would hope to get the following:

    func main() {
    var ints *[]int

    err := append1And2(ints); err != nil {
    panic(err)
    }

    fmt.Println(*ints) // should be an array of [1,2]
    This won't work. append1And2 gets a *copy* of the nil pointer and thus ints
    will stay nil.
    I'm providing only the nil value pointer, meaning I need to allocate the
    slice in the function. Is there any way of doing this?
    You can use the reflect.Append() function. It automacially initializes
    the slice if necessary.

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

    Harald

    --
    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.
  • Patrick-Ranjit D. Madsen at Jan 30, 2014 at 1:40 pm
    Harald, you beautiful man! You did in one line what I've been attempting to do in 20 so far. Thanks a bunch.
    On 30 Jan 2014, at 14:37, Harald Weidner wrote:

    Hello,
    I have a function that takes an interface{}, which must be a pointer to a
    slice (i of course check for this). What I want to do is the following:
    2) append the new element to the provided slice such that the original
    slice grows.
    For instance, ignoring the decoding (which I already have working), assume
    that all I wanted was to statically append the int values 1 and 2 to a
    *[]int. I would hope to get the following:

    func main() {
    var ints *[]int

    err := append1And2(ints); err != nil {
    panic(err)
    }

    fmt.Println(*ints) // should be an array of [1,2]
    This won't work. append1And2 gets a *copy* of the nil pointer and thus ints
    will stay nil.
    I'm providing only the nil value pointer, meaning I need to allocate the
    slice in the function. Is there any way of doing this?
    You can use the reflect.Append() function. It automacially initializes
    the slice if necessary.

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

    Harald

    --
    You received this message because you are subscribed to a topic in the Google Groups "golang-nuts" group.
    To unsubscribe from this topic, visit https://groups.google.com/d/topic/golang-nuts/Innhy9bhMJs/unsubscribe.
    To unsubscribe from this group and all its topics, 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
postedJan 30, '14 at 11:26a
activeJan 30, '14 at 1:40p
posts6
users3
websitegolang.org

People

Translate

site design / logo © 2022 Grokbase