Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- type Post struct{
- fieldName data `check:"value1"
- ...
- }
- type Post struct{
- fieldName data `check:"value2"
- ...
- }
- type Datastore interface {
- ...
- }
- func CheckSwitch(value reflect.Value){
- //this loops through the fields
- for i := 0; i < value.NumField(); i++ { // iterates through every struct type field
- tag := value.Type().Field(i).Tag // returns the tag string
- field := value.Field(i) // returns the content of the struct type field
- switch tag.Get("check"){
- case "value1":
- fmt.Println(field.String())//or some other function
- case "value2":
- fmt.Println(field.String())//or some other function
- ....
- }
- ///how could I modify the struct data during the switch seen above and then return the struct with the updated values?
- }
- }
- //the check function is used i.e
- function foo(){
- p:=Post{fieldName:"bar"}
- check(p)
- }
- func check(d Datastore){
- value := reflect.ValueOf(d) ///this gets the fields contained inside the struct
- CheckSwitch(value)
- ...
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement