The first recommendation I have is to make yourself a set of tests. In terms of this specific problem, it is likely because you're comparing pointers. Here is an alternate approach that doesn't use reflection, and deals with the pointers as well:
https://play.golang.org/p/9o7yGioRWYl I'd recommend you expand the tests to make sure you're happy with situations where the PmnId property is not given. (Please excuse the formatting - I'm on my phone). On Monday, 24 May 2021 at 16:44:34 UTC+1 Van Fury wrote: > Hi, > > I have an array of JSON objects as > > Structs: > ``` > type Data struct { > TaiList []Tai `json:"taiList"` > } > > type Tai struct { > PlmnId *PlmnId `json:"plmnId"` > > Tac string `json:"tac"` > > Nid string `json:"nid"` > } > > type PlmnId struct { > Mcc string `json:"mcc"` > > Mnc string `json:"mnc"` > } > ``` > > The JSON is of the form > > ``` > { > "taiList": [ > { > "plmnId": { > "mcc": "244", > "mnc": "24" > }, > "tac": "00001", > "nid": "99" > }, > { > "plmnId": { > "mcc": "244", > "mnc": "34" > }, > "tac": "00001", > "nid": "555" > } > ] > } > ``` > > I would like to check if the JSON object "ta" is contain in the "taiList". > > ``` > var ta = model.Tai{ > PlmnId: &model.PlmnId{Mcc: "244", Mnc: "34"}, > Tac: "00001", > Nid: "555", > } > ``` > > > I tried with the code below > > ``` > func CheckTai(tai model.Tai, TaiList []model.Tai) bool { > for _, Tai := range TaiList { > if reflect.DeepEqual(Tai, tai) { > return true > } > } > return false > } > > ``` > > but CheckTai function return false. > The CheckTai only return true when there is only one JSON object that > match the list as > > ``` > { > "taiList": [ > { > "plmnId": { > "mcc": "244", > "mnc": "24" > }, > "tac": "00001", > "nid": "555" > } > > ] > } > ``` > > Need help or idea on how to perform this check. > > -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/927937b9-5b8f-4f69-8886-491b84bfa106n%40googlegroups.com.
