工具包
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

44 rivejä
1.1 KiB

  1. package utils
  2. import "sort"
  3. func Struct2UrlParams(obj interface{}) string {
  4. var str = ""
  5. mapVal := Struct2Map(obj)
  6. var keys []string
  7. for key := range mapVal {
  8. keys = append(keys, key)
  9. }
  10. sort.Strings(keys)
  11. for _, key := range keys {
  12. str += key + "=" + AnyToString(mapVal[key]) + "&"
  13. }
  14. /*t := reflect.TypeOf(origin)
  15. v := reflect.ValueOf(origin)
  16. for i := 0; i < t.NumField(); i++ {
  17. tag := strings.ToLower(t.Field(i).Tag.Get("json"))
  18. if tag != "sign" {
  19. switch v.Field(i).Kind(){
  20. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  21. str += tag + "=" + utils.Int64ToStr(v.Field(i).Int()) + "&"
  22. break
  23. case reflect.String:
  24. str += tag + "=" + v.Field(i).String() + "&"
  25. break
  26. case reflect.Bool:
  27. str += tag + "=" + utils.BoolToStr(v.Field(i).Bool()) + "&"
  28. break
  29. case reflect.Float32, reflect.Float64:
  30. str += tag + "=" + utils.Float64ToStr(v.Field(i).Float())
  31. break
  32. case reflect.Array, reflect.Struct, reflect.Slice:
  33. str += ToUrlKeyValue(v.Field(i).Interface())
  34. default:
  35. break
  36. }
  37. }
  38. }*/
  39. return str
  40. }