Tuesday, July 7, 2015

Checking for nil in go (golang)

Well, am trying to check for nil in golang,

Looks like this utility may help

func IsNil(i interface{}) bool {
    if i == nil {
        return true
    }
    val := reflect.ValueOf(i)
    kind := val.Kind()
    switch kind {
    case reflect.Ptr, reflect.Chan, reflect.Func, reflect.Map, reflect.Slice, reflect.Interface:
        return val.IsNil()
    }
    return false
}
need to import "reflect" package.

No comments:

Post a Comment