Answer the question
In order to leave comments, you need to log in
go. How to return errors from a method?
The situation is the following. On http comes some complex data structure in the form of JSON. Inside are various dates as strings "2006-01-02". On the fly, I can’t convert these strings to time.Time, simply by specifying this type in the receiving structure, because there you can not specify a non-standard date format (at least I came to this conclusion. If I'm wrong, tell me how). Therefore I accept the data in the form of lines.
type input struct {
Renting_period period
Reservations reservations
}
type reservations []*period
type period struct {
Start date
End date
}
type date string
func (d date) toTime() time.Time {
timeObj, err := time.Parse("2006-01-02", string(d))
if err != nil {
panic(err)
}
return timeObj
}
...
reservation.End.toTime().AddDate(0, 0, 1)
...
func (r reservations) Len() int {
return len(r)
}
func (r reservations) Swap(a, b int) {
r[a], r[b] = r[b], r[a]
}
func (r reservations) Less(a, b int) bool {
return r[a].Start.toTime().Sub(r[b].Start.toTime()) < 0
}
Answer the question
In order to leave comments, you need to log in
func (d date) toTime() time.Time {
return time.Parse("2006-01-02", string(d))
}
...
time, err := reservation.End.toTime()
if err != nil {
...
}
time.AddDate(0, 0, 1)
...
func (r reservations) Less(a, b int) bool {
aTime, _ := r[a].Start.toTime()
bTime, _ := r[b].Start.toTime()
return aTime.Sub(bTime) < 0
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question