jeudi 8 août 2019

How to manipulate a map field in a structure with reflect package?

We have a structure like:

type S struct {
    M map[string]bool
}

And how can we implement a function like:


// this function modify field by name to a new map
func Modify(s *S, name string, val map[string]bool){
  ...
}

func Test() {
    s := S{
        M: map[string]bool{"Hello":true},
    }
    m := map[string]bool{"World":false}
    Modify(&s, "M", m)
}

The reflect package support SetInt/SetString/etc, but none SetMap. Any way to solve this problem?





Aucun commentaire:

Enregistrer un commentaire