FAQ
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

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

## Search Discussions

•  at Feb 28, 2013 at 4:33 am ⇧
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.

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

David
On Thursday, February 28, 2013 10:49:34 AM 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[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
--
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.
•  at Feb 28, 2013 at 4:33 am ⇧

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:
http://play.golang.org/p/OxHP8kEgGI

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

http://play.golang.org/p/08OzPRP1cS

my go version is 1.02 for windows.

thanks and regards,
dean
--
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.
•  at Feb 28, 2013 at 7:20 am ⇧

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.

--
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.
•  at Feb 28, 2013 at 9:12 pm ⇧

On 28 February 2013 18:20, John Asmuth 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.

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

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.
•  at Feb 28, 2013 at 11:16 pm ⇧
I think Dean is confused with array and slice.

David
On Friday, March 1, 2013 5:11:34 AM UTC+8, Andrew Gerrand wrote:

On 28 February 2013 18:20, John Asmuth <jas...@gmail.com <javascript:>>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.

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

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.
•  at Feb 28, 2013 at 11:47 pm ⇧
You might want to read this doc:
http://golang.org/doc/articles/slices_usage_and_internals.html

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

--
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.
•  at Mar 2, 2013 at 1:09 am ⇧
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

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

## Related Discussions

Discussion Overview
 group golang-nuts categories go posted Feb 28, '13 at 2:49a active Mar 2, '13 at 1:09a posts 8 users 5 website golang.org

### 5 users in discussion

Content

People

Support

Translate

site design / logo © 2021 Grokbase