Π Π°Π·Π±ΠΈΡ‚ΡŒ массив чисСл Π½Π° Π΄Π²Π° массива

102 views
Skip to first unread message

Vital Nabokov

unread,
Dec 1, 2019, 11:22:13β€―AM12/1/19
to golang-nuts
Π Π°Π·Π±ΠΈΡ‚ΡŒ массив чисСл Π½Π° Π΄Π²Π° массива?
Split an array of numbers into two arrays?

Marcin Romaszewicz

unread,
Dec 1, 2019, 12:08:02β€―PM12/1/19
to Vital Nabokov, golang-nuts
It's very simple to do that:



On Sun, Dec 1, 2019 at 8:22 AM Vital Nabokov <nabok...@gmail.com> wrote:
Π Π°Π·Π±ΠΈΡ‚ΡŒ массив чисСл Π½Π° Π΄Π²Π° массива?
Split an array of numbers into two 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...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/dc7702b6-9b12-4e2a-9295-78e821836ee5%40googlegroups.com.

Daniela Petruzalek

unread,
Dec 1, 2019, 3:35:04β€―PM12/1/19
to Marcin Romaszewicz, Vital Nabokov, golang-nuts
I'm not sure if that is a good ideaΒ πŸ˜…

https://play.golang.org/p/Mj77pgsaVsB

Daniela Petruzalek
Software Engineer


Marcin Romaszewicz

unread,
Dec 1, 2019, 3:40:29β€―PM12/1/19
to Daniela Petruzalek, Vital Nabokov, golang-nuts
Well, in that case:Β https://play.golang.org/p/DhSC_xF0jPWΒ  :)


Harald Weidner

unread,
Dec 1, 2019, 4:07:11β€―PM12/1/19
to golang-nuts
Hello,

> I'm not sure if that is a good idea πŸ˜…
> https://play.golang.org/p/Mj77pgsaVsB

This is why three-index slices were added in Go 1.2.

https://play.golang.org/p/_sJ9OKWsvMz

https://golang.org/doc/go1.2#three_index

Regards,
Harald

Bakul Shah

unread,
Dec 1, 2019, 5:56:05β€―PM12/1/19
to Harald Weidner, golang-nuts
Unfortunately a[i:j:k] acts exactly like a[i:j] if i+k >= cap(a)
and appending to subslice can mutate the underlying slice.

See https://play.golang.org/p/ppWob2yJDiS

Dan Kortschak

unread,
Dec 1, 2019, 9:34:18β€―PM12/1/19
to Bakul Shah, golang-nuts
What do you mean by i+k >= cap(a)? k+(any non-negative number) may not
be greater than cap(a). The key to safely appending when you are unsure
of the backing array's state is to append to the slice length and
capped to the same size.
Reply all
Reply to author
Forward
0 new messages