英文:
composite type and Pointer Methods
问题
我在创建一个基于time.Time类型的自定义类型Date时遇到了一些问题。
我尝试按照以下方式创建Date类型:
type Date time.Time
func (d Date) UnmarshalJSON(buf []byte) error {
var s string
json.Unmarshal(buf, &s)
t, err := time.Parse(timeLayout,s)
d= Date(t)
return err
}
func (d Date) MarshalJSON() ([]byte, error) {
b,_ := json.Marshal(d.Format(timeLayout))
return b,nil
}
这本身是可以工作的,我可以将这个Date作为time.Time类型存储在AppEngine Datastore中。
序列化本身也可以工作,
但是不工作的是:当从json反序列化时,Date d的值没有被填充。
这是因为,根据我正确理解的,unmarshalJson函数创建了一个Date的副本。
所以当我将unmarshalJson函数更改为使用指向Date的指针时,
我不能使用:
d=Date(t)
所以第一个问题是,有没有解决这个问题的方法?
我现在所做的是将代码重写如下:
type Date struct {
t time.Time
}
func (d *Date) UnmarshalJSON(buf []byte) error {
var s string
json.Unmarshal(buf, &s)
t, err := time.Parse(timeLayout,s)
d.t = t
return err
}
func (d Date) MarshalJSON() ([]byte, error) {
b,_ := json.Marshal(d.t.Format(timeLayout))
return b,nil
}
这样可以工作,但在这种情况下,Date不是time.Time类型的扩展类型,而只是对time.Time类型的包装。
有没有更好的解决方法?我对Go还很陌生。
我需要这个Date类型,以便拥有一个仅包含日期的json格式类型,因为Chrome只支持html5类型:date而不是datetime。
在Go中无法进行方法重写(即重写time.Time类型的un/marshalJson方法)吗?
谢谢
英文:
i have some problems creating a own type Date based on type time.Time
i tried to create the Date type as follows:
type Date time.Time
func (d Date) UnmarshalJSON(buf []byte) error {
var s string
json.Unmarshal(buf, &s)
t, err := time.Parse(timeLayout,s)
d= Date(t)
return err
}
func (d Date) MarshalJSON() ([]byte, error) {
b,_ := json.Marshal(d.Format(timeLayout))
return b,nil
}
this itself works, i can store this Date as a time.Time in AppEngine Datastore.
the marshaling itself also works,
but is not working is: then when unmarshal from json, the Date d is filled with the value.
this is, as i understand right, because the unmarshalJson function create a copy of Date.
so when i change the unmarshalJson function to use a Pointer to Date
then i cant use:
d=Date(t)
so first question, is there a solution how todo this ?
what i have done now is to rewrite the Code as follows:
type Date struct {
t time.Time
}
func (d *Date) UnmarshalJSON(buf []byte) error {
var s string
json.Unmarshal(buf, &s)
t, err := time.Parse(timeLayout,s)
d.t = t
return err
}
func (d Date) MarshalJSON() ([]byte, error) {
b,_ := json.Marshal(d.t.Format(timeLayout))
return b,nil
}
this works, but in this case Date is not an extending type of time.Time its just a wrapper to a time.Time type.
is there a better solution todo this ? im still new to go.
i need this Date type, to have a Date only json formated type, because Chrome only supports the html5 type: date and not datetime.
and method overriding is not possible in go (means to override the un/marshalJson methods of type time.Time ) ?
thanks
答案1
得分: 2
完全未经测试的代码:
type Date time.Time
func (d *Date) UnmarshalJSON(buf []byte) error {
var s string
json.Unmarshal(buf, &s)
t, err := time.Parse(timeLayout, s)
*d = *(*Date)(&t)
return err
}
func (d *Date) MarshalJSON() ([]byte, error) {
b, _ := json.Marshal(d.Format(timeLayout))
return b, nil
}
英文:
Totally untested code:
type Date time.Time
func (d *Date) UnmarshalJSON(buf []byte) error {
var s string
json.Unmarshal(buf, &s)
t, err := time.Parse(timeLayout, s)
*d = *(*Date)(&t)
return err
}
func (d *Date) MarshalJSON() ([]byte, error) {
b, _ := json.Marshal(d.Format(timeLayout))
return b, nil
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论