Created
July 11, 2018 03:49
-
-
Save JesseYan/e08948f4aa4a00c4d3972b596b500a2a to your computer and use it in GitHub Desktop.
Stringfy 将s转成string类型
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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