2017-12-29 11:31
浏览 105


I am working on a patch restful request that the body json contain some omitted value while sending to golang. Since an unset value will lead the golang struct become default value. So I would like to know if there are any solution to deal with patch request omit data?

As I know, a basic type like string / int cannot be nullable in golang. there are different approach to deal with unset value patch request. For example:

  1. using pointer to deal with null problem

    type User struct {
        Name *string
  2. using nullable library

    type User struct {
        Name  sql.NullString
  3. using map[string][]interface{} to see if the value is set

Is there any better solution to deal with nullable value inside struct? since this 3 should be work around to deal with the nullable value.

图片转代码服务由CSDN问答提供 功能建议

我正在处理一个补丁请求,请求主体json发送到golang时包含一些省略的值。 由于未设置的值将导致golang结构成为默认值。 因此,我想知道是否有解决补丁请求的任何解决方案?

据我所知,像string / int这样的基本类型在golang中不能为空。 有多种处理未设置值补丁请求的方法。 例如:

  1. 使用指针处理空问题

    名称 * string 
  2. 使用可空库

     使用 map [string] [] interface {} 命名sql.NullString 
        \ li> 
  3. 设置了值

    是否有更好的解决方案来处理struct中的可空值? 因为应该处理此3以处理可为空的值。

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • dongying9712 2017-12-29 12:08

    Use a pointer along with the omitempty tag. This way you can differentiate between an unset value, nil, and an intentional zero value ("", false, 0).

    type User struct {
        Name *string `json:"name,omitempty"`


    解决 无用
    打赏 举报
  • 查看更多回答(2条)

相关推荐 更多相似问题