A
A
Alexander Skipped2015-12-18 00:47:37
go
Alexander Skipped, 2015-12-18 00:47:37

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

As a result, the structure satisfies me quite well, except for a small detail. Those. I need to manipulate dates (sort, calculate intervals in days, etc.), then I need to convert the date string type to time.Time. I did this by creating a method for this type
func (d date) toTime() time.Time {
  timeObj, err := time.Parse("2006-01-02", string(d))
  if err != nil {
    panic(err)
  }
  return timeObj
}

And here lies the potential problem. In general, in a normal situation, everything is beautiful: I can access fields with a date, get values ​​\u200b\u200bin the form of a temporary object and manipulate it
...
reservation.End.toTime().AddDate(0, 0, 1)
...

But in the code of the method, it is easy to see the presence of a possible error and the call to panic. Naturally, the incoming data may contain a date in the wrong format or some other invalid data, respectively, I need to respond normally to an error and return an error response via http. And it seems that it was possible to make the method return not a single value, but also an error that should be processed in the calling function, but the problem is. that this method is called inside the sort sort. And there I did not understand whether it is possible to receive and transmit errors. For the same reason, I cannot pass w http.ResponseWriter to this method, because from the functions that implement the sorting interface, I don't know how to pass it to the method.
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
}

Perhaps the answer is obvious and my lack of experience in writing a web service in Go is affecting me, and for sure there is a correct way to resolve such things. That's why I'm asking for help. There is still a suspicion that it is possible to somehow convert the date type to the correct one in some other way, but I have not yet found it myself.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
Nikita, 2015-12-18
@Foxcool

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 question

Ask a Question

731 491 924 answers to any question