Skip to content

Instantly share code, notes, and snippets.

@r3b
Created July 22, 2014 21:30
Show Gist options
  • Save r3b/8b51ecb593ac3cf3466b to your computer and use it in GitHub Desktop.
Save r3b/8b51ecb593ac3cf3466b to your computer and use it in GitHub Desktop.
Turn any Go interface{} into a string
package stringify
import(
"fmt"
"reflect"
)
func Stringify(v reflect.Value) string {
var str string
switch v.Kind() {
case reflect.Func:
case reflect.Map:
case reflect.Slice:
return ""
case reflect.Array:
for i := 0; i < v.Len(); i++ {
if str == "" {
str = Stringify(v.Index(i))
} else {
str = fmt.Sprintf("%s, %s", str, Stringify(v.Index(i)))
}
}
case reflect.Struct:
for i := 0; i < v.NumField(); i++ {
if str == "" {
str = fmt.Sprintf("%s:%s", v.Type().Field(i).Name, Stringify(v.Field(i)))
} else {
str = fmt.Sprintf("%s, %s:%s", str, v.Type().Field(i).Name, Stringify(v.Field(i)))
}
}
}
return str
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment