From 382e99d06e8bdc2818704e4a22d23a11110803fa Mon Sep 17 00:00:00 2001 From: Jae Kwon Date: Mon, 2 Apr 2018 01:46:24 -0700 Subject: [PATCH] Add IsTypedNil --- common/nil.go | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 common/nil.go diff --git a/common/nil.go b/common/nil.go new file mode 100644 index 00000000..c7617f08 --- /dev/null +++ b/common/nil.go @@ -0,0 +1,18 @@ +package common + +import "reflect" + +// Go lacks a simple and safe way to see if something is a typed nil. +// See: +// - https://dave.cheney.net/2017/08/09/typed-nils-in-go-2 +// - https://groups.google.com/forum/#!topic/golang-nuts/wnH302gBa4I/discussion +// - https://github.com/golang/go/issues/21538 +func IsTypedNil(o interface{}) bool { + rv := reflect.ValueOf(o) + switch rv.Kind() { + case reflect.Chan, reflect.Func, reflect.Map, reflect.Ptr, reflect.Slice: + return rv.IsNil() + default: + return false + } +}