Home > Article > Backend Development > Does not rely on len() to check whether the initialized slice is empty
I have a use case where the built-in len() function is not suitable for checking if the initialized (non-zero) slice is empty.
How to safely check if a slice is empty without len()?
I try to index the first element, however, if it doesn't exist, my program panics.
There is absolutely no reason not to use len()
, so just use len since len is an O(1) operation .
The above is the detailed content of Does not rely on len() to check whether the initialized slice is empty. For more information, please follow other related articles on the PHP Chinese website!