Files
gf/g/internal/empty/empty.go

57 lines
1.8 KiB
Go
Raw Normal View History

// Copyright 2019 gf Author(https://github.com/gogf/gf). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.
package empty
import (
2019-06-05 18:40:26 +08:00
"reflect"
)
// 判断给定的变量是否为空。
// 整型为0, 布尔为false, slice/map长度为0, 其他为nil的情况都为空。
// 为空时返回true否则返回false。
func IsEmpty(value interface{}) bool {
if value == nil {
return true
}
// 优先通过断言来进行常用类型判断
switch value := value.(type) {
case int: return value == 0
case int8: return value == 0
case int16: return value == 0
case int32: return value == 0
case int64: return value == 0
case uint: return value == 0
case uint8: return value == 0
case uint16: return value == 0
case uint32: return value == 0
case uint64: return value == 0
case float32: return value == 0
case float64: return value == 0
case bool: return value == false
case string: return value == ""
case []byte: return len(value) == 0
default:
2019-06-05 18:40:26 +08:00
// Finally using reflect.
rv := reflect.ValueOf(value)
2019-06-05 18:40:26 +08:00
switch rv.Kind() {
case reflect.Chan,
reflect.Map,
reflect.Slice,
reflect.Array:
return rv.Len() == 0
2019-06-05 18:40:26 +08:00
case reflect.Func,
reflect.Ptr,
reflect.Interface,
reflect.UnsafePointer:
if rv.IsNil() {
return true
}
}
}
return false
}