FAQ
Hi all,

Just curious, why the following statements make a compile error: "cannot
assign to slice[2:]"?

         slice := []int{1, 2}
slice[2:] = append(slice[2:], 3)

slice[2:] is also a slice, and it conforms to the definition of append:

         func append(slice []Type, elems ...Type) []Type

Thanks in advance!

Best Regards
Nan Xiao

--
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 Mar 21, 2016 at 1:16 am

    On Mon, Mar 21, 2016 at 12:08 PM, Nan Xiao wrote:
    Hi all,

    Just curious, why the following statements make a compile error: "cannot
    assign to slice[2:]"?

    slice := []int{1, 2}
    slice[2:] = append(slice[2:], 3)
    slice[2:] is a value, it's not a variable.
    What would you expect the result of this statement to be?

    --
    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.
  • Peter Weinberger at Mar 21, 2016 at 1:24 am
    There are languages in which that would work (e.g., R) to replace part of
    the slice, but Go is not such a language.

    On Sun, Mar 20, 2016 at 9:15 PM, Jesse McNelis wrote:
    On Mon, Mar 21, 2016 at 12:08 PM, Nan Xiao wrote:
    Hi all,

    Just curious, why the following statements make a compile error: "cannot
    assign to slice[2:]"?

    slice := []int{1, 2}
    slice[2:] = append(slice[2:], 3)
    slice[2:] is a value, it's not a variable.
    What would you expect the result of this statement to be?

    --
    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.
  • Nan Xiao at Mar 21, 2016 at 1:35 am
    Hi Jesse,

    I just want to replace the value of the slice.E.g.:

    slice := []int{1, 2}
    slice[2:] = append(slice[2:], 3)

    just want to add 3 in the slice[2].

    Best Regards
    Nan Xiao
    On Mon, Mar 21, 2016 at 9:23 AM, Peter Weinberger wrote:

    There are languages in which that would work (e.g., R) to replace part of
    the slice, but Go is not such a language.

    On Sun, Mar 20, 2016 at 9:15 PM, Jesse McNelis wrote:

    On Mon, Mar 21, 2016 at 12:08 PM, Nan Xiao <xiaonan830818@gmail.com>
    wrote:
    Hi all,

    Just curious, why the following statements make a compile error: "cannot
    assign to slice[2:]"?

    slice := []int{1, 2}
    slice[2:] = append(slice[2:], 3)
    slice[2:] is a value, it's not a variable.
    What would you expect the result of this statement to be?

    --
    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.
  • Andrew Gerrand at Mar 21, 2016 at 1:49 am
    append returns a slice value, which must be assigned to a slice variable.

    slice[2:] is a slice value, not a slice variable.

    I think

       slice = append(slice[2:], 3)

    will do what you want.


    On 21 March 2016 at 12:34, Nan Xiao wrote:

    Hi Jesse,

    I just want to replace the value of the slice.E.g.:

    slice := []int{1, 2}
    slice[2:] = append(slice[2:], 3)

    just want to add 3 in the slice[2].

    Best Regards
    Nan Xiao
    On Mon, Mar 21, 2016 at 9:23 AM, Peter Weinberger wrote:

    There are languages in which that would work (e.g., R) to replace part of
    the slice, but Go is not such a language.


    On Sun, Mar 20, 2016 at 9:15 PM, Jesse McNelis <jessta@jessta.id.au>
    wrote:
    On Mon, Mar 21, 2016 at 12:08 PM, Nan Xiao <xiaonan830818@gmail.com>
    wrote:
    Hi all,

    Just curious, why the following statements make a compile error: "cannot
    assign to slice[2:]"?

    slice := []int{1, 2}
    slice[2:] = append(slice[2:], 3)
    slice[2:] is a value, it's not a variable.
    What would you expect the result of this statement to be?

    --
    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.
    --
    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.
  • Matt Harden at Mar 21, 2016 at 2:20 am
    Actually it sounds like Nan Xiao just wants to append to the slice:

    slice = append(slice, 3)

    After this, slice will contain {1, 2, 3}.
    On Sun, Mar 20, 2016, 18:49 Andrew Gerrand wrote:

    append returns a slice value, which must be assigned to a slice variable.

    slice[2:] is a slice value, not a slice variable.

    I think

    slice = append(slice[2:], 3)

    will do what you want.


    On 21 March 2016 at 12:34, Nan Xiao wrote:

    Hi Jesse,

    I just want to replace the value of the slice.E.g.:

    slice := []int{1, 2}
    slice[2:] = append(slice[2:], 3)

    just want to add 3 in the slice[2].

    Best Regards
    Nan Xiao
    On Mon, Mar 21, 2016 at 9:23 AM, Peter Weinberger wrote:

    There are languages in which that would work (e.g., R) to replace part
    of the slice, but Go is not such a language.


    On Sun, Mar 20, 2016 at 9:15 PM, Jesse McNelis <jessta@jessta.id.au>
    wrote:
    On Mon, Mar 21, 2016 at 12:08 PM, Nan Xiao <xiaonan830818@gmail.com>
    wrote:
    Hi all,

    Just curious, why the following statements make a compile error: "cannot
    assign to slice[2:]"?

    slice := []int{1, 2}
    slice[2:] = append(slice[2:], 3)
    slice[2:] is a value, it's not a variable.
    What would you expect the result of this statement to be?

    --
    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.
    --
    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.
  • Nan Xiao at Mar 21, 2016 at 3:20 am
    Hi Matt,

    I am not just wanting to append a value, but curious about why slice[2:]
    can't be used
    as the left-value. As Andrew has pointed out, "slice[2:] is a slice value,
    not a slice variable.".

    Thanks!


    Best Regards
    Nan Xiao
    On Mon, Mar 21, 2016 at 10:19 AM, Matt Harden wrote:

    Actually it sounds like Nan Xiao just wants to append to the slice:

    slice = append(slice, 3)

    After this, slice will contain {1, 2, 3}.
    On Sun, Mar 20, 2016, 18:49 Andrew Gerrand wrote:

    append returns a slice value, which must be assigned to a slice variable.

    slice[2:] is a slice value, not a slice variable.

    I think

    slice = append(slice[2:], 3)

    will do what you want.


    On 21 March 2016 at 12:34, Nan Xiao wrote:

    Hi Jesse,

    I just want to replace the value of the slice.E.g.:

    slice := []int{1, 2}
    slice[2:] = append(slice[2:], 3)

    just want to add 3 in the slice[2].

    Best Regards
    Nan Xiao

    On Mon, Mar 21, 2016 at 9:23 AM, Peter Weinberger <pjw@golang.org>
    wrote:
    There are languages in which that would work (e.g., R) to replace part
    of the slice, but Go is not such a language.


    On Sun, Mar 20, 2016 at 9:15 PM, Jesse McNelis <jessta@jessta.id.au>
    wrote:
    On Mon, Mar 21, 2016 at 12:08 PM, Nan Xiao <xiaonan830818@gmail.com>
    wrote:
    Hi all,

    Just curious, why the following statements make a compile error: "cannot
    assign to slice[2:]"?

    slice := []int{1, 2}
    slice[2:] = append(slice[2:], 3)
    slice[2:] is a value, it's not a variable.
    What would you expect the result of this statement to be?

    --
    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.
    --
    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.
  • Gary Willoughby at Mar 24, 2016 at 3:09 pm
    The answer, I think, is in your question. A slice (such as slice[2:]) is a
    reference to the underlying storage and not the storage itself so it can't
    be used. In this case, it's an rvalue
    (https://en.wikipedia.org/wiki/R-value), not a lvalue
    (https://en.wikipedia.org/wiki/L-value).
    On Monday, 21 March 2016 03:20:49 UTC, Nan Xiao wrote:

    Hi Matt,

    I am not just wanting to append a value, but curious about why slice[2:]
    can't be used
    as the left-value. As Andrew has pointed out, "slice[2:] is a slice
    value, not a slice variable.".

    Thanks!


    Best Regards
    Nan Xiao

    On Mon, Mar 21, 2016 at 10:19 AM, Matt Harden <matt....@gmail.com
    <javascript:>> wrote:
    Actually it sounds like Nan Xiao just wants to append to the slice:

    slice = append(slice, 3)

    After this, slice will contain {1, 2, 3}.

    On Sun, Mar 20, 2016, 18:49 Andrew Gerrand <a...@golang.org <javascript:>>
    wrote:
    append returns a slice value, which must be assigned to a slice variable.

    slice[2:] is a slice value, not a slice variable.

    I think

    slice = append(slice[2:], 3)

    will do what you want.



    On 21 March 2016 at 12:34, Nan Xiao <xiaona...@gmail.com <javascript:>>
    wrote:
    Hi Jesse,

    I just want to replace the value of the slice.E.g.:

    slice := []int{1, 2}
    slice[2:] = append(slice[2:], 3)

    just want to add 3 in the slice[2].

    Best Regards
    Nan Xiao

    On Mon, Mar 21, 2016 at 9:23 AM, Peter Weinberger <p...@golang.org
    <javascript:>> wrote:
    There are languages in which that would work (e.g., R) to replace part
    of the slice, but Go is not such a language.


    On Sun, Mar 20, 2016 at 9:15 PM, Jesse McNelis <jes...@jessta.id.au
    <javascript:>> wrote:
    On Mon, Mar 21, 2016 at 12:08 PM, Nan Xiao <xiaona...@gmail.com
    <javascript:>> wrote:
    Hi all,

    Just curious, why the following statements make a compile error: "cannot
    assign to slice[2:]"?

    slice := []int{1, 2}
    slice[2:] = append(slice[2:], 3)
    slice[2:] is a value, it's not a variable.
    What would you expect the result of this statement to be?

    --
    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...@googlegroups.com <javascript:>.
    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...@googlegroups.com <javascript:>.
    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...@googlegroups.com <javascript:>.
    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
postedMar 21, '16 at 1:08a
activeMar 24, '16 at 3:09p
posts8
users6
websitegolang.org

People

Translate

site design / logo © 2022 Grokbase