go.charczuk.com@v0.0.0-20240327042549-bc490516bd1a/sdk/assert/nil.go (about)

     1  /*
     2  
     3  Copyright (c) 2023 - Present. Will Charczuk. All rights reserved.
     4  Use of this source code is governed by a MIT license that can be found in the LICENSE file at the root of the repository.
     5  
     6  */
     7  
     8  package assert
     9  
    10  import "reflect"
    11  
    12  // Nil returns if a given reference is nil, but also returning true
    13  // if the reference is a valid typed pointer to nil, which may not strictly
    14  // be equal to nil.
    15  func Nil(object any) bool {
    16  	if object == nil {
    17  		return true
    18  	}
    19  
    20  	value := reflect.ValueOf(object)
    21  	kind := value.Kind()
    22  	if kind >= reflect.Chan && kind <= reflect.Slice && value.IsNil() {
    23  		return true
    24  	}
    25  	return false
    26  }