Advertisement
Guest User

Untitled

a guest
Oct 21st, 2016
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.11 KB | None | 0 0
  1. type Post struct{
  2. fieldName data `check:"value1"
  3. ...
  4. }
  5.  
  6. type Post struct{
  7. fieldName data `check:"value2"
  8. ...
  9. }
  10.  
  11. type Datastore interface {
  12. ...
  13. }
  14.  
  15. func CheckSwitch(value reflect.Value){
  16. //this loops through the fields
  17. for i := 0; i < value.NumField(); i++ { // iterates through every struct type field
  18. tag := value.Type().Field(i).Tag // returns the tag string
  19. field := value.Field(i) // returns the content of the struct type field
  20.  
  21. switch tag.Get("check"){
  22. case "value1":
  23. fmt.Println(field.String())//or some other function
  24. case "value2":
  25. fmt.Println(field.String())//or some other function
  26. ....
  27.  
  28. }
  29. ///how could I modify the struct data during the switch seen above and then return the struct with the updated values?
  30.  
  31.  
  32. }
  33. }
  34.  
  35. //the check function is used i.e
  36. function foo(){
  37. p:=Post{fieldName:"bar"}
  38. check(p)
  39. }
  40.  
  41. func check(d Datastore){
  42. value := reflect.ValueOf(d) ///this gets the fields contained inside the struct
  43. CheckSwitch(value)
  44.  
  45. ...
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement