在接收器方法中将空指针赋值为nil。

huangapple go评论100阅读模式
英文:

Assign to nil pointer in receiver method

问题

我正在尝试在将使用JSON编码/解码的结构中使用time.Time结构。如果time.Time属性未设置,它们不应包含在内(使用omitempty标签),因此为了能够实现这一点,我将不得不使用指向time.Time对象的指针。

我已经为time.Time结构定义了一个类型,这样我就可以轻松地创建接收函数,以在编码和解码JSON时格式化时间等。

在这里可以看到代码:https://play.golang.org/p/e81xzA-dzz

因此,在我的主要结构(实际上将被编码的结构)中,我将这样做:

type EncodeThis struct {
   Str  string   `json:"str,omitempty"`
   Date *jWDate  `json:"date,omitempty"`
}

问题是指针可能为nil,在尝试解码值时,因此如果您查看我在Go Playground上的代码,您会看到我正在尝试(使用双指针)设置接收器的地址,如果它为nil。请参阅"Set"和"swap"方法。

但是,这似乎不起作用。程序不会失败或出现任何错误,但"EncodeThis"结构将不包含对这个新地址的引用。有没有什么修复这个问题的想法?

英文:

I'm trying to use a time.Time structure in a structure that will be encoded/decoded with JSON. The time.Time attributes should not be included if they aren't set (omitempty tag), so to be able to do so I will have to use a pointer to the time.Time object.

I have defined a type for the time.Time structure so I easily can create receiver functions format the time when the JSON is encoded and decoded etc.

See the code here: https://play.golang.org/p/e81xzA-dzz

So in my main structure (the structure that actually will be encoded) I will do something like this:

type EncodeThis struct {
   Str string `json:"str,omitempty"`
   Date *jWDate `json:"date,omitempty"`
}

The problem is that the pointer may be nil, when trying to decode the value, so if you look at my code at the Go playground, you can see that I'm trying to (using double pointers) to set the address of the receiver if its nil. See method "Set" and "swap".

But, this doesnt seem to work. The program doesn't fail or anything, but the "EncodeThis" struct will not contain a reference to this new address. Any idea for a fix for this?

答案1

得分: 2

将你的日期对象包装在一个包含指向time.Time对象的指针的结构体中。

// JWDate: 数字日期值
type jWDate struct {
    date *time.Time
}

func (jwd *jWDate) Set(t *time.Time) {
    if jwd.date == nil {
        jwd.date = t
    }
}

如果你需要从jWDate结构体中访问time.Time的方法,你可以将其嵌入。通过嵌入类型,你仍然可以轻松访问对象的指针:

// JWDate: 数字日期值
type jWDate struct {
    *time.Time // 嵌入的`time.Time`类型指针,而不仅仅是一个属性
}

func (jwd *jWDate) Set(t *time.Time) {
    if jwd.Time == nil {
        jwd.Time = t
    }
}
英文:

Wrap your date object with a struct containing a pointer to time.Time object.

// JWDate: Numeric date value
type jWDate struct {
    date *time.Time
}

func (jwd *jWDate) Set(t *time.Time) {
    if jwd.date == nil {
	    jwd.date = t
    }
}

If you need to have access to time.Time methods from jWDate struct you can embed it. With embedded type you still have ease access to an object's pointer:

// JWDate: Numeric date value
type jWDate struct {
    *time.Time // Embedded `time.Time` type pointer, not just an attribute
}

func (jwd *jWDate) Set(t *time.Time) {
    if jwd.Time == nil {
	    jwd.Time = t
	}
}

huangapple
  • 本文由 发表于 2017年3月9日 17:28:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/42691509.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定