I am unmarshalling a JSON response into a struct. For one of the fields, it returns an int and a string when empty.
type example struct {
Position int `json:"position"`
}
json: cannot unmarshal string into Go struct field .position of type int
The response is either
{"position":8} or {"position":"none"}
How can I handle both an int and string response?
Change the type to interface{}
, and then you can check the type at runtime.
type example struct {
Position interface{} `json:"position"`
}
/*
Returns an int and a bool, indicating if a position exists.
*/
func (e * example) getValue() (int,bool){
if v,ok := Position.(int) {
return v,true
} else {
return 0,false
}
}