[英]Golang - Returning null value when return type is string
I have a go struct something like this我有一个像这样的 go 结构
type Country struct {
WhoAllAreComing []string `json:"attendees"`
NameOfThePlace string `json:"name"`
EventDate string `json:"eventDate"`
}
This is the response struct that I need to send back after ingesting the input and manipulating it.这是我在摄取输入并对其进行操作后需要发回的响应结构。
Now if suppose the EventDate
is empty, I need to pass a null in the json response and not an empty string.现在,如果假设
EventDate
为空,我需要在 json 响应中传递null而不是空字符串。 How do we convert an empty string in go to a null in json response.我们如何将 go 中的空字符串转换为 json 响应中的 null。
Using json.Marshall
to marshall my response into Json is just converting it to empty string for EventDate.使用
json.Marshall
将我的响应编组为 Json 只是将其转换为 EventDate 的空字符串。
Use a pointer to return a null instead of an empty string.使用指针返回 null 而不是空字符串。
type Country struct {
WhoAllAreComing []string `json:"attendees"`
NameOfThePlace string `json:"name"`
EventDate *string `json:"eventDate"`
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.