github.com/richardwilkes/toolbox@v1.121.0/nil.go (about) 1 // Copyright (c) 2016-2024 by Richard A. Wilkes. All rights reserved. 2 // 3 // This Source Code Form is subject to the terms of the Mozilla Public 4 // License, version 2.0. If a copy of the MPL was not distributed with 5 // this file, You can obtain one at http://mozilla.org/MPL/2.0/. 6 // 7 // This Source Code Form is "Incompatible With Secondary Licenses", as 8 // defined by the Mozilla Public License, version 2.0. 9 10 package toolbox 11 12 import "reflect" 13 14 // IsNil returns true if the interface is nil or if the value it points to is nil. 15 func IsNil(i any) bool { 16 if i == nil { 17 return true 18 } 19 switch reflect.TypeOf(i).Kind() { 20 case reflect.Chan, 21 reflect.Func, 22 reflect.Interface, 23 reflect.Map, 24 reflect.Pointer, 25 reflect.Slice, 26 reflect.UnsafePointer: 27 return reflect.ValueOf(i).IsNil() 28 default: 29 return false 30 } 31 }