Go has a different understanding of slice with python, why go think like this?

1,311 views
Skip to first unread message

Dean Sinaean

unread,
Feb 27, 2013, 9:49:34 PM2/27/13
to golan...@googlegroups.com

In go :
A is an array
B:=A[:]
or
B:=A
then B is a copy of A

B:=A[2:5]
then B is a pointer to A[2]  (I mean when an element in B is modified, the corresponding element in A is also modified.)

in python, its just the  opposite way. why does go think  its way is better?

ps:
what is the difference between B:=A[0:]  and B:=A[0:6] when A has seven elements? in the latter case, I found A modified when B is modified, while in the former case, A is not.

my go version is 1.02 for windows.

thanks and regards,
dean

David DENG

unread,
Feb 27, 2013, 11:25:09 PM2/27/13
to golan...@googlegroups.com
if len(A) == 7, A[:]/A[0:] is a slice of length 7, while A[0:6]'s length == 6. They are all not hard copy.


David

David Leimbach

unread,
Feb 27, 2013, 11:25:14 PM2/27/13
to golan...@googlegroups.com


On Wednesday, February 27, 2013 6:49:34 PM UTC-8, Dean Sinaean wrote:

In go :
A is an array
B:=A[:]
or
B:=A
then B is a copy of A

B := A[:] makes a slice of A, containing all of a and assigns it to B.  These two structures can have potentially different indexing, but share the same backing memory storage of the original array in A.  It's not true to say "B is a copy of A".  I guess you can call it a shallow copy in some instances, but I don't think of "shallow copies" as real copies very often. 


See this:

 

B:=A[2:5]
then B is a pointer to A[2]  (I mean when an element in B is modified, the corresponding element in A is also modified.)

in python, its just the  opposite way. why does go think  its way is better?

Opposite?   Can you demonstrate?  I really don't remember this part of Python very well.
 

ps:
what is the difference between B:=A[0:]  and B:=A[0:6] when A has seven elements? in the latter case, I found A modified when B is modified, while in the former case, A is not.


I don't see that at all...

John Asmuth

unread,
Feb 28, 2013, 2:20:08 AM2/28/13
to golan...@googlegroups.com


On Wednesday, February 27, 2013 9:49:34 PM UTC-5, Dean Sinaean wrote:

then B is a copy of A

 

what is the difference between B:=A[0:]  and B:=A[0:6] when A has seven elements? in the latter case, I found A modified when B is modified, while in the former case, A is not.

 Your experiments are not working. Nothing you did ever copied any arrays.

Andrew Gerrand

unread,
Feb 28, 2013, 4:11:34 PM2/28/13
to John Asmuth, golang-nuts

On 28 February 2013 18:20, John Asmuth <jas...@gmail.com> wrote:
 Your experiments are not working. Nothing you did ever copied any arrays.

Not true. He mentioned

A is an array
B := A

which is an array copy.


Andrew

David DENG

unread,
Feb 28, 2013, 6:16:44 PM2/28/13
to golan...@googlegroups.com, John Asmuth
I think Dean is confused with array and slice.

David

Andrew Gerrand

unread,
Feb 28, 2013, 6:47:22 PM2/28/13
to Dean Sinaean, golang-nuts
You might want to read this doc:

It explains how arrays and slices work in Go. Perhaps from that you'll gather some idea of why they were designed that way. The main thing is giving the programmer explicit control over memory allocations.

Andrew

Dean Sinaean

unread,
Mar 1, 2013, 8:09:40 PM3/1/13
to golang-nuts
Sorry for late reply, I found the problem at work, where I can't access gmail via a computer and I sent this mail using my phone.

Back at home, I can't reproduce the case in which B is a copy of A in go anyhow. Maybe I made a mistake( sorry..).

With go, B:=A, and I make some changes to B, for example B[1]=6, the corresponding element in A is changed, too.
package main
import(
    "fmt"
)
func main(){
    var A=[]int{1,2,3,4,5}//or var A=[5]int{1,2,3,4,5}
    B:=A// or B:=A[0:] or B:=A[:]
    B[1]=6
    fmt.Println(A)
}

the result is [1 6 3 4 5]。

while with Python:
>>> A=[1,2,3,4,5]
>>> B=A[2:]
>>> B[1]=6
>>> A
[1, 2, 3, 4, 5]
>>> B=A
>>> B[1]=6
>>> A
[1, 6, 3, 4, 5]

I think I'm clear with the difference now, and I better use copy function to copy a slice.
Thank you all for the conversation.

Nice weekend,
Dean
Reply all
Reply to author
Forward
0 new messages