mh_server/lib/requests/common.go
2021-06-30 10:12:05 +08:00

70 lines
1.2 KiB
Go

package requests
import (
"reflect"
"strconv"
)
type ContentType string
var (
JSON ContentType = "application/json"
Form ContentType = "application/x-www-form-urlencoded"
XML ContentType = "text/xml"
)
func switchToString(in interface{}) (str string, err error) {
switch in.(type) {
case int:
str = strconv.Itoa(in.(int))
return
case int8:
str = strconv.Itoa(int(in.(int8)))
return
case int16:
str = strconv.Itoa(int(in.(int16)))
return
case int32:
str = strconv.Itoa(int(in.(int32)))
return
case int64:
str = strconv.Itoa(int(in.(int64)))
return
case uint:
str = strconv.Itoa(int(in.(uint)))
return
case uint8:
str = strconv.Itoa(int(in.(uint8)))
return
case uint16:
str = strconv.Itoa(int(in.(uint16)))
return
case uint32:
str = strconv.Itoa(int(in.(uint32)))
return
case uint64:
str = strconv.Itoa(int(in.(uint64)))
return
case float32:
str = strconv.FormatFloat(float64(in.(float32)), 'f', 6, 32)
return
case float64:
str = strconv.FormatFloat(in.(float64), 'f', 6, 64)
return
case bool:
if reflect.ValueOf(in).Bool() {
str = "true"
return
} else {
str = "false"
return
}
case string:
str = in.(string)
return
default:
err = UnSupportTypeValue
return
}
}