3 ms·
I'm a bit confused. caption is supposed to be length of underlying array of a slice. What does it mean, concepturally, to create a new slice out of an existing
by songgao 13y ago
I'm a bit confused. caption is supposed to be length of underlying array of a slice. What does it mean, concepturally, to create a new slice out of an existing one but with a different length of underlying array?
Another thing is, maybe I'm worrying too much but, what if Go becomes more and more complicated that simplicity is not one of its advantages anymore, and people who're new to the language will get scared?
- lucian1900 13y agoWhile I don't think the slice changes are particularly out of character for Go, its simplicity is in general somewhat superficial. The type system is unsound and lacks power (although the interfaces are mostly nice), there are no generics (except for builtin collections), error handling is hard to compose (except in non-idiomatic ways), no real type inference (although single assignments are easier). Its simplicity is not composable.
- yiyus 13y agoWell, conceptually it means you limit how much you can extend the length of the slice inside the array, not that the array has a different length. For example, we have: full := []int{0, 1, 2, 3, 4, 5} half1 := full[:3] // [0, 1, 2] half2 := full[3:] // [3, 4, 5] Now, the capacity of half1 is 6, so it can "grow inside" half2, such that: append(half1, 666) // Now, half2 is [666, 4, 5] This may happen even when you are not using append, for example you could do: half1 = half1[:5] and get into half2 again. The new syntax solves this problem such that, after defining half1 as full[:3:3], if you try half1[:5] you will get an error (the length cannot be larger than the capacity) and if you use append, a new array will be allocated, leaving the contents of half2 intact in any case.