var a = [...]int{0, 1, 2, 3, 4, 5, 6, 7} var s = make([]int, 6) var b = make([]byte, 5) n1 := copy(s, a[0:]) // n1 == 6, s == []int{0, 1, 2, 3, 4, 5} n2 := copy(s, s[2:]) // n2 == 4, s == []int{2, 3, 4, 5, 4, 5} n3 := copy(b, "Hello, World!") // n3 == 5, b == []byte("Hello")
Ian
On Saturday, October 26, 2019 at 9:47:39 PM UTC+2, Ian Lance Taylor wrote:On Sat, Oct 26, 2019 at 10:50 AM atd...@gmail.com <atd...@gmail.com> wrote:
>
> I get that we can do that but it's not strictly about slices operations such as appending or copying.
> Of course, we could copy slices all the time but then there is no benefit in having them and it is not ergonomic as your example shows, nor does it fit your performance cost requirements.
>
> If you look at the code snippet I posted, I am merely assigning a value of type slice to a struct field. Now, that struct can be copied a number of times without its actual value changing. It looks immutable in some sense. BUT, it's actually implicitly tied to an object that is mutable (backing array) and nowhere have we made sure that the mutation of this backing array is safe.
For better or for worse, the Go language has shared state. This is
most obvious when using pointers: if you copy a pointer, you do not
copy the memory to which that pointer points. If that is clear, then
the thing to remember about slices is that they are pointers.
Pointers in Go do not permit arithmetic.Slices in Go are pointers
that permit bounded arithmetic.
Oh, I hadn't thought about it that way! Interesting.
I still feel like there should be a way to reign in the amount of shared state in the cases of these "fat" pointers a little bit. Or at least, inspect it, warn about it more because most people seem to think that slices are just infinite arrays (hence the need for the extra-level of indirection/pointer).
But perhaps, the coding patterns/idioms make it a non issue. For future considerations I guess.
I believe it's going to be to hard to write a go vet analyser that could warn to detect unintended state change on a array from two different pointers.
--
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.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/30ac16bc-bc70-40a6-bc79-f41e3536d144%40googlegroups.com.
On Sun, 27 Oct 2019, 00:04 Gert, <gert....@gmail.com> wrote:I believe it's going to be to hard to write a go vet analyser that could warn to detect unintended state change on a array from two different pointers.Isn't that why we have the race detector?
--
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.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/39c04bde-2c9c-4396-84c1-a71143de5e39%40googlegroups.com.
This is not necessarily due to races. You can have this exact situation
occurring in single threaded code. I don't think any mention of race
issues was made here.
On Sun, 27 Oct 2019, 21:31 Dan Kortschak, <d...@kortschak.io> wrote:This is not necessarily due to races. You can have this exact situation
occurring in single threaded code. I don't think any mention of race
issues was made here.The thread subject mentions goroutines and the original message talks specifically about concurrent access, which sounds very much like it's race-related to me.
On Sun, 2019-10-27 at 08:53 +0000, roger peppe wrote:
> On Sun, 27 Oct 2019, 00:04 Gert, <gert....@gmail.com> wrote:
>
> > I believe it's going to be to hard to write a go vet analyser that
> > could
> > warn to detect unintended state change on a array from two
> > different
> > pointers.
> >
>
> Isn't that why we have the race detector?
>
> --
> > 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 golan...@googlegroups.com.