Add IsNil function for checking nil values in Go #399
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
This pull request introduces the
IsNil
function, which checks if a value isnil
or if it's a reference type with anil
underlying value. TheIsNil
function is useful in cases where a simple equality check (x == nil
) might not be sufficient to determine the nilness of a value.Context
In Go, the behaviour of
x == nil
can vary based on the type ofx
. A more nuanced check is required to identify whether the underlying value is ' nil ' for reference types such as pointers, slices, maps, channels, functions, and interfaces. Thereflect
package provides theIsNil
method, employed in thisIsNil
function to handle such cases.Pre-Go 1.20 interfaces can't use [T comparable] so I did the check using the regular approach without Generics, I think in this case doesn't make much difference to have it using Generic the end goal is the same, receiving something and checking if that something is IsNil, without the need to push go version to v1.20