r3b
7/22/2014 - 9:30 PM

Turn any Go interface{} into a string

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
}