Without iterating over the entire array, how can I check if x
in array using Go? Does the language have a construct?
Like Python: if "x" in array: ...
goif statement
Without iterating over the entire array, how can I check if x
in array using Go? Does the language have a construct?
Like Python: if "x" in array: ...
Best Answer
There is no built-in operator to do it in Go. You need to iterate over the array. You can write your own function to do it, like this:
If you want to be able to check for membership without iterating over the whole list, you need to use a map instead of an array or slice, like this: