From 381a8e639606c61b9eb4f947f6ac8843f0159571 Mon Sep 17 00:00:00 2001 From: Dmitry Selivanov Date: Fri, 14 Jul 2023 15:47:54 +0200 Subject: [PATCH] fix: refactor delete from slice --- services_list.go | 2 +- utils.go | 7 ++++--- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/services_list.go b/services_list.go index ae96ca6..da7dafb 100644 --- a/services_list.go +++ b/services_list.go @@ -326,7 +326,7 @@ func (l *ServicesList) RemoveFromHealthyByIndex(i int) { logger.Log().Warn(fmt.Errorf("unexpected error during service Close(): %w", err).Error()) } - l.healthy = append(l.healthy[:i], l.healthy[i+1:]...) + l.healthy = deleteFromSlice(l.healthy, i) } // RemoveFromJail remove given diff --git a/utils.go b/utils.go index 93c39ec..259b762 100644 --- a/utils.go +++ b/utils.go @@ -21,7 +21,8 @@ func Sleep(t time.Duration, cancelCh <-chan struct{}) { // deleteFromSlice delete item with // given index from provided slice func deleteFromSlice(slice []service.IService, index int) []service.IService { - copy(slice[index:], slice[index+1:]) - slice[len(slice)-1] = nil - return slice[:len(slice)-1] + //https://stackoverflow.com/questions/37334119/how-to-delete-an-element-from-a-slice-in-golang + temp := make([]service.IService, 0) + temp = append(temp, slice[:index]...) + return append(temp, slice[index+1:]...) }