FAQ
Is it true that slices/arrays/maps/structs always get passed/returned by
reference (i.e. not by value)?
The following small code snippet demonstrates that slices do get returned
by reference:
http://play.golang.org/p/bVP5wPCcQ2

If I am overgeneralizing, could you please tell me what the rule is?
Thanks!
Meir

--

Search Discussions

  • Patrick Mylund Nielsen at Sep 21, 2012 at 2:09 pm
    Slices are passed by value, but slices are very small, essentially just a
    pointer to an underlying array. That pointer is the reference.
    On Fri, Sep 21, 2012 at 10:02 AM, Meir wrote:


    Is it true that slices/arrays/maps/structs always get passed/returned by
    reference (i.e. not by value)?
    The following small code snippet demonstrates that slices do get returned
    by reference:
    http://play.golang.org/p/bVP5wPCcQ2

    If I am overgeneralizing, could you please tell me what the rule is?
    Thanks!
    Meir

    --

    --
  • Jan Mercl at Sep 21, 2012 at 2:14 pm
    On Fri, Sep 21, 2012 at 4:02 PM, Meir wrote:

    Arrays and structs are value types.

    Slice is a value type BUT its backing array may be shared w/ other
    slices. One can think about passing a slice as shallow copying.

    type S []T

    is like

    type S struct{
    len, cap int
    elems *[X]T // where X, len, cap are set/change by make,
    append, [a:b] etc.
    }

    -j

    --
  • Jan Mercl at Sep 21, 2012 at 2:24 pm
    On Fri, Sep 21, 2012 at 4:14 PM, Jan Mercl wrote:

    s/X/cap/g

    see also: http://research.swtch.com/godata

    -j

    --
  • Andrey mirtchovski at Sep 21, 2012 at 2:58 pm
  • Kevin Gillette at Sep 21, 2012 at 7:56 pm
    In other words, *everything* in go is pass-by-value. You just need to know
    what the nature of that value is (a copy of a pointer is a lot different
    than a copy of what's pointed to).
    On Friday, September 21, 2012 8:02:18 AM UTC-6, Meir wrote:


    Is it true that slices/arrays/maps/structs always get passed/returned by
    reference (i.e. not by value)?
    The following small code snippet demonstrates that slices do get returned
    by reference:
    http://play.golang.org/p/bVP5wPCcQ2

    If I am overgeneralizing, could you please tell me what the rule is?
    Thanks!
    Meir
    --
  • Bryanturley at Sep 21, 2012 at 8:37 pm
    Sounds like what you really want is a new copy of an existing slice, which
    you could do with the make and copy builtins.
    I might be reading to much from your question.

    You probably want to pass larger structs by reference, it is just not
    automagically done for you.
    new(SomeType) returns a pointer/reference to a newly allocated struct, not
    the struct itself for instance.

    You can pass by reference with the same syntax as c mostly.
    On Friday, September 21, 2012 2:49:45 PM UTC-5, Kevin Gillette wrote:

    In other words, *everything* in go is pass-by-value. You just need to
    know what the nature of that value is (a copy of a pointer is a lot
    different than a copy of what's pointed to).
    On Friday, September 21, 2012 8:02:18 AM UTC-6, Meir wrote:


    Is it true that slices/arrays/maps/structs always get passed/returned by
    reference (i.e. not by value)?
    The following small code snippet demonstrates that slices do get returned
    by reference:
    http://play.golang.org/p/bVP5wPCcQ2

    If I am overgeneralizing, could you please tell me what the rule is?
    Thanks!
    Meir
    --
  • Job van der Zwan at Sep 22, 2012 at 11:59 am

    On Friday, 21 September 2012 21:49:45 UTC+2, Kevin Gillette wrote:

    In other words, *everything* in go is pass-by-value. You just need to
    know what the nature of that value is (a copy of a pointer is a lot
    different than a copy of what's pointed to).

    Just like Java! :P
    http://javadude.com/articles/passbyvalue.htm

    --
  • Meir Goldenberg at Sep 22, 2012 at 5:54 pm
    Indeed. Thank you!
    http://play.golang.org/p/KXepSqvxje
    On Fri, Sep 21, 2012 at 10:49 PM, Kevin Gillette wrote:

    In other words, *everything* in go is pass-by-value. You just need to
    know what the nature of that value is (a copy of a pointer is a lot
    different than a copy of what's pointed to).

    On Friday, September 21, 2012 8:02:18 AM UTC-6, Meir wrote:


    Is it true that slices/arrays/maps/structs always get passed/returned by
    reference (i.e. not by value)?
    The following small code snippet demonstrates that slices do get returned
    by reference:
    http://play.golang.org/p/**bVP5wPCcQ2<http://play.golang.org/p/bVP5wPCcQ2>

    If I am overgeneralizing, could you please tell me what the rule is?
    Thanks!
    Meir
    --

    --
  • Rory McGuire at Sep 21, 2012 at 9:09 pm
    I believe slices work like the code below:

    type slice struct {
    start *uintptr
    length int64
    capacity int64
    }
    example := make([]byte, 0, 10) // slice{start:calloc(*10*), length:*0*,
    capacity:*10*}

    return example // return the VALUE slice{start:calloc(*10*), length:*0*,
    capacity:*10*}

    example2 := make([]byte, len(example), cap(example))
    copy(example2, example)
    return example2 // return the VALUE slice{start:calloc(*10*), length:*0*,
    capacity:*10*} but its a new COPY

    On Friday, 21 September 2012 16:02:18 UTC+2, Meir wrote:


    Is it true that slices/arrays/maps/structs always get passed/returned by
    reference (i.e. not by value)?
    The following small code snippet demonstrates that slices do get returned
    by reference:
    http://play.golang.org/p/bVP5wPCcQ2

    If I am overgeneralizing, could you please tell me what the rule is?
    Thanks!
    Meir
    --
  • Minux at Sep 22, 2012 at 4:18 am

    On Sep 22, 2012 5:09 AM, "Rory McGuire" wrote:
    I believe slices work like the code below:

    type slice struct {
    start *uintptr
    length int64
    capacity int64
    }
    len and cap are typed int, not int64.
    this structure is called SliceHeader in
    package reflect.

    --
  • Rory McGuire at Sep 22, 2012 at 5:52 am
    Okay. Thanks for the quick reply.
    On Sep 22, 2012 7:48 AM, "minux" wrote:

    On Sep 22, 2012 1:37 PM, "Rory McGuire" wrote:
    Ah right, otherwise one might have problems on 32bit platforms?
    len and cap is int because this is the default integer type.
    Is it correct that on amd64 int is 64 bits?
    it will be, but right now, it is 32 bits.
    --

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedSep 21, '12 at 2:02p
activeSep 22, '12 at 5:54p
posts12
users9
websitegolang.org

People

Translate

site design / logo © 2022 Grokbase