github.com/searKing/golang/go@v1.2.117/reflect/nil.go (about)

     1  // Copyright 2020 The searKing Author. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package reflect
     6  
     7  import "reflect"
     8  
     9  // IsNil reports whether its argument v is a nil interface value or an untyped nil.
    10  // Note that IsNil is not always equivalent to a regular comparison with nil in Go.
    11  // It is equivalent to:
    12  //
    13  //	var typedNil any = (v's underlying type)(nil)
    14  //	return v == nil || v == typedNil
    15  //
    16  // For example, if v was created by set with `var p *int` or calling IsNil((*int)(nil)),
    17  // i==nil will be false but [IsNil] will be true.
    18  func IsNil(v any) bool {
    19  	if v == nil {
    20  		return true
    21  	}
    22  	return IsNilValue(reflect.ValueOf(v))
    23  }
    24  
    25  // UnTypeNil returns its argument v or nil if and only if v is a nil interface value or an untyped nil.
    26  func UnTypeNil(v any) any {
    27  	// convert typed nil into untyped nil
    28  	if IsNil(v) {
    29  		return nil
    30  	}
    31  	return v
    32  }
    33  
    34  // Deprecated: Use [IsNil] instead.
    35  func IsNilObject(i any) (result bool) {
    36  	return IsNil(i)
    37  }