github.com/JohanShen/go-utils@v1.1.4-0.20201117124024-901319a2b2a0/utils/tostr.go (about)

     1  package utils
     2  
     3  import (
     4  	"fmt"
     5  	"reflect"
     6  	"strings"
     7  )
     8  
     9  // 判断对象是否为空
    10  func IsObjectNil(obj interface{}) bool {
    11  	// 对象是否为空
    12  	// 空引用
    13  	if obj == nil {
    14  		return true
    15  	}
    16  	// 指针类型 判断指向的内存是否为空
    17  	vof := reflect.ValueOf(obj)
    18  	if vof.Kind() == reflect.Ptr {
    19  		//fmt.Printf("obj = %v, %v, %#v", vof.IsNil(), vof.IsZero(),reflect.ValueOf(obj).Elem())
    20  		//|| vof.Elem().Len()==0
    21  		if vof.IsNil() || vof.IsZero() {
    22  			return true
    23  		}
    24  	} else {
    25  		// 字符串类型判断
    26  		val := fmt.Sprintf("%v", obj)
    27  		if val == "<nil>" {
    28  			return true
    29  		}
    30  	}
    31  	return false
    32  }
    33  
    34  var spaceChar = []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32}
    35  
    36  // 判断字符串是否为空或空字符串
    37  func IsStrEmptyOrNull(str string) bool {
    38  	if len(str) == 0 {
    39  		return true
    40  	}
    41  	for _, v := range spaceChar {
    42  		str = strings.Trim(str, string(v))
    43  		//fmt.Printf("V = %#v , %#v \n",string(v), v)
    44  	}
    45  	return len(str) == 0
    46  }
    47  
    48  // 任意类型转成字符串
    49  func AnyToStr(obj interface{}) string {
    50  	if obj == nil {
    51  		return ""
    52  	}
    53  	val := ""
    54  	vof := reflect.ValueOf(obj)
    55  	if vof.Kind() == reflect.Ptr {
    56  		if vof.IsNil() || vof.IsZero() {
    57  			val = fmt.Sprint("")
    58  			//fmt.Printf("obj = %v, %v, %#v", vof.IsNil(), vof.IsZero(),reflect.ValueOf(obj).Elem())
    59  		} else {
    60  			val1 := vof.Elem()
    61  			val = fmt.Sprintf("%v", val1)
    62  		}
    63  	} else {
    64  		val = fmt.Sprintf("%v", obj)
    65  
    66  		if len(val) == 0 || val == "<nil>" {
    67  			if val = reflect.TypeOf(obj).String(); len(val) == 0 {
    68  				val = reflect.TypeOf(obj).Kind().String()
    69  			}
    70  		}
    71  	}
    72  	return val
    73  }
    74  
    75  // 转换成字符串
    76  // 代码保留 研究用
    77  //func AnyToStr1(a interface{}) string {
    78  //	b := ""
    79  //
    80  //	switch a.(type) {
    81  //	case int:
    82  //		b = strconv.FormatInt(int64(a.(int)), 10)
    83  //	case int8:
    84  //		b = strconv.FormatInt(int64(a.(int8)), 10)
    85  //	case int16:
    86  //		b = strconv.FormatInt(int64(a.(int16)), 10)
    87  //	case int32:
    88  //		b = strconv.FormatInt(int64(a.(int32)), 10)
    89  //	case int64:
    90  //		b = strconv.FormatInt(a.(int64), 10)
    91  //	case float32:
    92  //		b = fmt.Sprintf("%f", a)
    93  //		//b = strconv.FormatFloat(float64(a.(float32)), 'f', -1, 64)
    94  //	case float64:
    95  //		b = fmt.Sprintf("%f", a)
    96  //		b = strconv.FormatFloat(a.(float64), 'f', -1, 64)
    97  //	case string:
    98  //		b = a.(string)
    99  //	default:
   100  //		str := reflect.ValueOf(a)
   101  //		switch strings.ToLower(reflect.TypeOf(a).Kind().String()) {
   102  //		case "int","int8","int16","int32","int64":
   103  //
   104  //			vtype := reflect.TypeOf(int64(0))
   105  //			r1 := str.Convert(vtype).Int()
   106  //			b = strconv.FormatInt(r1, 10)
   107  //
   108  //		case "uint","uint8","uint16","uint32","uint64":
   109  //
   110  //			vtype := reflect.TypeOf(uint64(0))
   111  //			r1 := str.Convert(vtype).Uint()
   112  //			b = strconv.FormatUint(r1, 10)
   113  //		case "string":
   114  //
   115  //			vtype := reflect.TypeOf(string(0))
   116  //			r1 := str.Convert(vtype).String()
   117  //			b = r1
   118  //
   119  //		default:
   120  //			b = GetTypeName(a)
   121  //		}
   122  //	}
   123  //	return b
   124  //}