Graphql optional fileds in golang

In my graphql schema there are several optional fields

cityId: Int!
amount: Int!
loanTerm: Int!
schemeId: Int
historyId: Int
earlyRepayment: Boolean
earningsConfirmation: Boolean

and in My golang i Have this struct

   CityID               int   `json:"cityId"`
Amount               int   `json:"amount"`
LoanTerm             int   `json:"loanTerm"`
SchemeID             *int  `json:"schemeId"`
HistoryID            *int  `json:"historyId"`
EarlyRepayment       *bool `json:"earlyRepayment"`
EarningsConfirmation *bool `json:"earningsConfirmation"`

as you can see optional fields have pointer to int (*int) should I check them for nil value before uisng?

Yes you must. Because if you try to use a pointer variables pointed-to value and the pointer is nil vill you get a panic in your program. So

var p *int
...
if p != nil {
    a := *p + 32
}

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.