简体   繁体   English

Golang - 返回类型为字符串时返回 null 值

[英]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.

相关问题 在 BigQuery 中加载 avro 文件 - 默认值的意外类型。 预期为 null,但找到了字符串:“null” - Load a avro file in BigQuery - Unexpected type for default value. Expected null, but found string: "null" 必须返回非空值,因为返回类型“UserCredentialPlatform”不允许空值 - A non-null value must be returned since the return type 'UserCredentialPlatform' doesn't allow null golang如何将接口类型(总是基本类型)覆盖到字符串 - golang how to cover an interface type (always basic type) to string 无法将类型“[String: Any]”的返回表达式转换为返回类型“Song?” - Cannot convert return expression of type '[String : Any]' to return type 'Song?' 我可以用未知类型的golang中的标签复制接口指针的值吗 - Can I copy the value of pointer of interface with tags in golang with unknown type Android Firebase 一个值从实时数据库返回 null - Android Firebase a value is returning null from Realtime Database FireBase getUid 在返回值后返回为 Null - FireBase getUid Coming Back As Null After Returning a Value 字符串拆分 Golang - String Split Golang _TypeError 在构建 NotesListView(dirty) 时被抛出:“Null”类型不是“String”类型的子类型 - _TypeError was thrown building NotesListView(dirty) : type 'Null' is not a subtype of type 'String' Golang如何在gRPC中返回列表 - Golang how to return list in gRPC
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM