Exclude returns a new slice containing the items from base that do not appear in the exclude set. Order of remaining items is preserved. Always returns a fresh slice (never aliases base) even when no items are removed.
(base []T, exclude ...T)
| 121 | // in the exclude set. Order of remaining items is preserved. |
| 122 | // Always returns a fresh slice (never aliases base) even when no items are removed. |
| 123 | func Exclude[T comparable](base []T, exclude ...T) []T { |
| 124 | if len(exclude) == 0 { |
| 125 | return append([]T(nil), base...) |
| 126 | } |
| 127 | |
| 128 | excluded := make(map[T]struct{}, len(exclude)) |
| 129 | for _, item := range exclude { |
| 130 | excluded[item] = struct{}{} |
| 131 | } |
| 132 | |
| 133 | result := make([]T, 0, len(base)) |
| 134 | for _, item := range base { |
| 135 | if _, isExcluded := excluded[item]; !isExcluded { |
| 136 | result = append(result, item) |
| 137 | } |
| 138 | } |
| 139 | return result |
| 140 | } |
no outgoing calls