github.com/wtfutil/wtf@v0.43.0/utils/reflective.go (about) 1 package utils 2 3 import ( 4 "fmt" 5 "reflect" 6 ) 7 8 // StringValueForProperty returns a string value for the given property 9 // If the property doesn't exist, it returns an error 10 func StringValueForProperty(ref interface{}, propName string) (string, error) { 11 v := reflect.ValueOf(ref) 12 refVal := reflect.Indirect(v).FieldByName(propName) 13 14 if !refVal.IsValid() { 15 return "", fmt.Errorf("invalid property name: %s", propName) 16 } 17 18 strVal := fmt.Sprintf("%v", refVal) 19 20 return strVal, nil 21 }