github.com/gogf/gf@v1.16.9/internal/utils/utils_is.go (about)

     1  // Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
     2  //
     3  // This Source Code Form is subject to the terms of the MIT License.
     4  // If a copy of the MIT was not distributed with this file,
     5  // You can obtain one at https://github.com/gogf/gf.
     6  
     7  package utils
     8  
     9  import (
    10  	"github.com/gogf/gf/internal/empty"
    11  	"reflect"
    12  )
    13  
    14  // IsNil checks whether `value` is nil.
    15  func IsNil(value interface{}) bool {
    16  	return value == nil
    17  }
    18  
    19  // IsEmpty checks whether `value` is empty.
    20  func IsEmpty(value interface{}) bool {
    21  	return empty.IsEmpty(value)
    22  }
    23  
    24  // IsInt checks whether `value` is type of int.
    25  func IsInt(value interface{}) bool {
    26  	switch value.(type) {
    27  	case int, *int, int8, *int8, int16, *int16, int32, *int32, int64, *int64:
    28  		return true
    29  	}
    30  	return false
    31  }
    32  
    33  // IsUint checks whether `value` is type of uint.
    34  func IsUint(value interface{}) bool {
    35  	switch value.(type) {
    36  	case uint, *uint, uint8, *uint8, uint16, *uint16, uint32, *uint32, uint64, *uint64:
    37  		return true
    38  	}
    39  	return false
    40  }
    41  
    42  // IsFloat checks whether `value` is type of float.
    43  func IsFloat(value interface{}) bool {
    44  	switch value.(type) {
    45  	case float32, *float32, float64, *float64:
    46  		return true
    47  	}
    48  	return false
    49  }
    50  
    51  // IsSlice checks whether `value` is type of slice.
    52  func IsSlice(value interface{}) bool {
    53  	var (
    54  		reflectValue = reflect.ValueOf(value)
    55  		reflectKind  = reflectValue.Kind()
    56  	)
    57  	for reflectKind == reflect.Ptr {
    58  		reflectValue = reflectValue.Elem()
    59  	}
    60  	switch reflectKind {
    61  	case reflect.Slice, reflect.Array:
    62  		return true
    63  	}
    64  	return false
    65  }
    66  
    67  // IsMap checks whether `value` is type of map.
    68  func IsMap(value interface{}) bool {
    69  	var (
    70  		reflectValue = reflect.ValueOf(value)
    71  		reflectKind  = reflectValue.Kind()
    72  	)
    73  	for reflectKind == reflect.Ptr {
    74  		reflectValue = reflectValue.Elem()
    75  	}
    76  	switch reflectKind {
    77  	case reflect.Map:
    78  		return true
    79  	}
    80  	return false
    81  }
    82  
    83  // IsStruct checks whether `value` is type of struct.
    84  func IsStruct(value interface{}) bool {
    85  	var (
    86  		reflectValue = reflect.ValueOf(value)
    87  		reflectKind  = reflectValue.Kind()
    88  	)
    89  	for reflectKind == reflect.Ptr {
    90  		reflectValue = reflectValue.Elem()
    91  		reflectKind = reflectValue.Kind()
    92  	}
    93  	switch reflectKind {
    94  	case reflect.Struct:
    95  		return true
    96  	}
    97  	return false
    98  }