I tend to be quite careful around removing items from an array/slice/list 
and not just in Go.

Deletion of items is probably the most mutable thing you can do to a list - 
if the list is shared between goroutines, it could really mess things up.
Rather than delete, I'd suggest a mark and copy approach instead, where you 
run through the slice once making a "To Delete" list, and copy those not 
scheduled for deletion into a new list. Its a tad inefficient, two loops 
through the user list, with a secondary allocation for your 'To Delete' 
list but I think it's fairly safe.

https://play.golang.org/p/Fu3QlEKb-t - has a full mark and copy sweep and a 
second version that does a single sweep with check and copy in the single 
loop

Calum

On Monday, 20 November 2017 16:48:31 UTC, Trig wrote:
>
> for i, user := range myList {
>  if user.Disabled { 
>   myList = append(myList[:i], myList[i + 1:]...) // remove user from 
> return list 
>  } 
> }
>
>
> Is using append this way (to remove an index), inside of the range loop of 
> the same array I'm working with, safe... since the size of myList is being 
> changed within it?
>

-- 
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+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to