FAQ
Is it correct if I think of Slices like this: When I pass a slice to a function, the slice is copied but the copy the function receives has a copy of the pointer to the backing array not a copy of the backing array itself.
--
--
rottmann.net | Google+ <https://plus.google.com/106658337982640166595> |
Twitter <http://twitter.com/ralf> | Facebook<http://www.facebook.com/ralfrottmann>|
LinkedIn <http://de.linkedin.com/in/rottmann>

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

  • Rob Pike at Apr 26, 2014 at 9:16 pm
    blog.golang.org/slices

    --
    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.
  • Ralf Rottmann at Apr 26, 2014 at 9:19 pm
    Rob, I read all of that. :-) just wanted to understand the internals a bit better!
    --
    --
    rottmann.net | Google+ <https://plus.google.com/106658337982640166595> |
    Twitter <http://twitter.com/ralf> | Facebook<http://www.facebook.com/ralfrottmann>|
    LinkedIn <http://de.linkedin.com/in/rottmann>

    --
    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.
  • Ilya Kostarev at Jul 18, 2014 at 8:59 pm
    Very neat article. Just my opinion, things should be expressed in the language specification. Specification hasn’t notion of reference types. Specification explicitly say that arguments are passed by value, assuming but not explicit copy semantic. So one can expect argument’s original can’t be modified inside function.

    On Apr 27, 2014, at 1:15 AM, Rob Pike wrote:


    blog.golang.org/slices

    --
    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.
  • Rodrigo Kochenburger at Jul 18, 2014 at 9:10 pm
    It is by value. When you pass the object to a function it will create a
    copy of it, including pointers, which mean the copy will be pointing to the
    same objects the original was

    A slice is nothing but a pointer to an array with some metadata.

    - RK

    On Fri, Jul 18, 2014 at 1:59 PM, Ilya Kostarev wrote:

    Very neat article. Just my opinion, things should be expressed in the
    language specification. Specification hasn’t notion of reference types.
    Specification explicitly say that arguments are passed by value, assuming
    but not explicit copy semantic. So one can expect argument’s original can’t
    be modified inside function.

    On Apr 27, 2014, at 1:15 AM, Rob Pike wrote:


    blog.golang.org/slices

    --
    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.
  • Rogerpcs at Jul 18, 2014 at 10:34 pm
    The slice header is passed, either by value or as a reference.
    The array is not copied.
    Roger
    On Friday, July 18, 2014 5:10:14 PM UTC-4, Rodrigo Kochenburger wrote:

    It is by value. When you pass the object to a function it will create a
    copy of it, including pointers, which mean the copy will be pointing to the
    same objects the original was

    A slice is nothing but a pointer to an array with some metadata.

    - RK


    On Fri, Jul 18, 2014 at 1:59 PM, Ilya Kostarev <uveli...@gmail.com
    <javascript:>> wrote:
    Very neat article. Just my opinion, things should be expressed in the
    language specification. Specification hasn’t notion of reference types.
    Specification explicitly say that arguments are passed by value, assuming
    but not explicit copy semantic. So one can expect argument’s original can’t
    be modified inside function.

    On Apr 27, 2014, at 1:15 AM, Rob Pike <r...@golang.org <javascript:>>
    wrote:


    blog.golang.org/slices

    --
    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.
  • Matt Harrison at Jul 19, 2014 at 7:56 am
    The slice header is passed, either by value or as a reference.

    If you have a function which accepts a slice as an argument, that slice
    will always be passed by value (i.e copied).

    If you want to modify the slice header in your function for any reason you
    should have your argument accept a pointer to a slice instead: *[]byte
    On Friday, July 18, 2014 11:28:20 PM UTC+1, roge...@gmail.com wrote:

    The slice header is passed, either by value or as a reference.
    The array is not copied.
    Roger
    On Friday, July 18, 2014 5:10:14 PM UTC-4, Rodrigo Kochenburger wrote:

    It is by value. When you pass the object to a function it will create a
    copy of it, including pointers, which mean the copy will be pointing to the
    same objects the original was

    A slice is nothing but a pointer to an array with some metadata.

    - RK


    On Fri, Jul 18, 2014 at 1:59 PM, Ilya Kostarev <uveli...@gmail.com>
    wrote:
    Very neat article. Just my opinion, things should be expressed in the
    language specification. Specification hasn’t notion of reference types.
    Specification explicitly say that arguments are passed by value, assuming
    but not explicit copy semantic. So one can expect argument’s original can’t
    be modified inside function.

    On Apr 27, 2014, at 1:15 AM, Rob Pike wrote:


    blog.golang.org/slices

    --
    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.
    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.
    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.
  • Minux at Apr 26, 2014 at 9:25 pm

    On Sat, Apr 26, 2014 at 5:11 PM, Ralf Rottmann wrote:

    Is it correct if I think of Slices like this: When I pass a slice to a
    function, the slice is copied but the copy the function receives has a copy
    of the pointer to the backing array not a copy of the backing array itself.
    yes. internally slices are represented as a struct of sliceheader, which
    contains the pointer,
    len and capacity, when you pass that slice to another function, the
    sliceheader will be passed
    by value to that function, so that function can change the content of the
    slice, but not the
    len or capacity as their change won't propagate back to the caller.

    another good read is: research.swtch.com/godata

    --
    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
postedApr 26, '14 at 9:11p
activeJul 19, '14 at 7:56a
posts8
users7
websitegolang.org

People

Translate

site design / logo © 2022 Grokbase