I have this code in my project:
//data.Objects is a map[string]interface{}
corporateName := data.Objects["corporateName"].(string)
dba := data.Objects["dba"].(string)
licence := data.Objects["licence"].(string)
resaleCert := data.Objects["resaleCert"].(string)
einNumber := data.Objects["einNumber"].(string)
phoneNumber := data.Objects["phoneNumber"].(string)
website := data.Objects["website"].(string)
contactName := data.Objects["contactName"].(string)
email := data.Objects["email"].(string)
billingAddress := data.Objects["billingStreet"].(string)
billingCity := data.Objects["billingCity"].(string)
billingCode := data.Objects["billingCode"].(int64)
billingState := data.Objects["billingState"].(string)
billingCountry := data.Objects["billingContry"].(string)
deliveryAddress := data.Objects["deliveryAddress"].(string)
In the declared map, some of these key couldn't have a value associated so they can return nil. If they do that I'm not able to do a type conversion. I'm looking for a way in order to improve this access and prevent the incorrect conversion.
Using some if else to check it it's quite annoying and verbose. Is there a way in order to avoid it?
N.B. The nil values for the structure are perfectly legal in this case so it's not a problem that I can avoid before.