slices.go

/* Copyright © 2019, 2020, 2021, 2022 Red Hat, Inc. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */

Package collections contains helper functions to work with collections.

package
collections

Documentation in literate-programming-style is available at: https://redhatinsights.github.io/insights-operator-utils/packages/collections/slices.html


StringInSlice tests whether the given string is contained in slice of strings. If the slice is empty, false is returned.

func
StringInSlice
(
a
string
,
list
[
]
string
)
bool
{
for
_
,
b
:=
range
list
{
if
b
==
a
{
return
true
}
}
return
false
}

Index finds given key in sequence. Second return value indicates if key was found or not. If the sequence is empty, zero index is returned with found flag set to false.

func
Index
(
key
string
,
list
[
]
string
)
(
int
,
bool
)
{
for
i
,
item
:=
range
list
{
if
item
==
key
{
return
i
,
true
}
}

any index is ok to return

	
return
0
,
false
}