Skip to content

Instantly share code, notes, and snippets.

@JesseYan
Created July 11, 2018 03:49
Show Gist options
  • Save JesseYan/e08948f4aa4a00c4d3972b596b500a2a to your computer and use it in GitHub Desktop.
Save JesseYan/e08948f4aa4a00c4d3972b596b500a2a to your computer and use it in GitHub Desktop.
Stringfy 将s转成string类型
package utils
import (
"reflect"
"strconv"
)
// Stringfy 将s转成string类型
func Stringfy(s interface{}) string {
v := reflect.ValueOf(s)
k := reflect.ValueOf(s).Kind()
switch k {
case reflect.String:
v := v.String()
return v
case reflect.Int, reflect.Int32, reflect.Int64:
v := strconv.FormatInt(v.Int(), 10)
return v
case reflect.Float32, reflect.Float64:
v := strconv.FormatFloat(v.Float(), 'f', 4, 10)
return v
default:
panic("origin value is not a Stringfy type origin value")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment