FAQ
Hi,

I am unable to understand why copy is not copying the slice contents below.

http://play.golang.org/p/4Ag7Bmw7q2

package main

import "fmt"

func main() {
     a := make([]int, 0, 10)
     a = append(a, 1)
     fmt.Println(a)
     b := make([]int, 0, 10)
     fmt.Println(copy(b, a))
     fmt.Println(b)
}

Can someone explain if this is the intended behavior or is it a bug ?

Sankar

--
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.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/72324abd-6805-40d0-a7b0-042433568fdb%40googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Search Discussions

  • Andrew Gerrand at Oct 14, 2013 at 9:06 am
    Copy doesn't care about capacity, just length.

    Your slice b has zero length, so you can't copy anything into it.
    copy(b[:1], a) would work. Make sure you also print b[:1] or you won't see
    the copied data. :-)

    Your question indicates a misunderstanding about how slices work. You
    should read this article: http://blog.golang.org/slices

    Andrew

    --
    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.
    To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/CAHxS-ounVctSeLnY_A3hbwbs67KYiun4SZojq8RMpe_eJPGZBw%40mail.gmail.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Dave Cheney at Oct 14, 2013 at 9:06 am
    You need to make sure the destination slice has enough space (not potential) to accept the copy

    http://play.golang.org/p/nrry4lAhMn
    On 14 Oct 2013, at 19:52, Sankar wrote:

    Hi,

    I am unable to understand why copy is not copying the slice contents below.

    http://play.golang.org/p/4Ag7Bmw7q2

    package main

    import "fmt"

    func main() {
    a := make([]int, 0, 10)
    a = append(a, 1)
    fmt.Println(a)
    b := make([]int, 0, 10)
    fmt.Println(copy(b, a))
    fmt.Println(b)
    }

    Can someone explain if this is the intended behavior or is it a bug ?

    Sankar
    --
    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.
    To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/72324abd-6805-40d0-a7b0-042433568fdb%40googlegroups.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.
    To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/0FB5A0C4-9674-4FAA-8770-CF4BFD8536B3%40cheney.net.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Sankar P at Oct 14, 2013 at 9:09 am
    ah okay.

    Thanks for the explanation. I was deceived by the fact that the cap(b)
    is non-zero. Now everything is clear.
    On Mon, Oct 14, 2013 at 2:36 PM, Dave Cheney wrote:
    You need to make sure the destination slice has enough space (not potential)
    to accept the copy

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

    On 14 Oct 2013, at 19:52, Sankar wrote:

    Hi,

    I am unable to understand why copy is not copying the slice contents below.

    http://play.golang.org/p/4Ag7Bmw7q2

    package main

    import "fmt"

    func main() {
    a := make([]int, 0, 10)
    a = append(a, 1)
    fmt.Println(a)
    b := make([]int, 0, 10)
    fmt.Println(copy(b, a))
    fmt.Println(b)
    }

    Can someone explain if this is the intended behavior or is it a bug ?

    Sankar

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

    To view this discussion on the web visit
    https://groups.google.com/d/msgid/golang-nuts/72324abd-6805-40d0-a7b0-042433568fdb%40googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.


    --
    Sankar P
    http://psankar.blogspot.com

    --
    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.
    To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/CAMSEaH7T0_Ju0Myrp%2B9DHx6EYf%2BgwBx7%3DuY1qAFwjKKwrz9tgg%40mail.gmail.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Chris dollin at Oct 14, 2013 at 9:08 am
    On 14 October 2013 09:52, Sankar wrote:

    Hi,
    I am unable to understand why copy is not copying the slice contents below.

    http://play.golang.org/p/4Ag7Bmw7q2

    package main

    import "fmt"

    func main() {
    a := make([]int, 0, 10)
    a = append(a, 1)
    fmt.Println(a)
    b := make([]int, 0, 10)
    fmt.Println(copy(b, a))
    fmt.Println(b)
    }

    Can someone explain if this is the intended behavior or is it a bug ?
      http://golang.org/ref/spec#Appending_and_copying_slices

    "The number of elements copied is the minimum of len(src) and len(dst)."

    len(b) is zero.

    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.
    To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/CAPFYLYgtfQmGcJKvVHEG72DTkpp94Vtu4HPrW00VwKaU%2Bc6QVA%40mail.gmail.com.
    For more options, visit https://groups.google.com/groups/opt_out.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedOct 14, '13 at 8:53a
activeOct 14, '13 at 9:09a
posts5
users4
websitegolang.org

People

Translate

site design / logo © 2022 Grokbase