#Go track - help working with time.Parse

5 messages · Page 1 of 1 (latest)

radiant mango
#

Hey everyone, I'm working through https://exercism.org/tracks/go/exercises/booking-up-for-beauty
I'm struggling with the first task.

// Schedule returns a time.Time from a string containing a date.
func Schedule(date string) (time.Time) {
    return time.Parse("01/02/2006 15:04:00", date)
}

I'm getting an error because time.Parse returns a time.Time but also returns an error.
I need to only return the first return value from time.Parse, but can't work out how. Any pointers are appreciated.

Exercism

Can you solve Booking up for Beauty in Go? Improve your Go skills with support from our world-class team of mentors.

sudden zinc
#

I'm not a very good Go programmer but I think there are three problems:

  • time.Parse returns two values, the Time and an error.
  • The layout string has very specific values that tell time.Parse the position of the day, month, year, etc. That's the reason for that odd timestamp "01/02/2006 15:04:05" (1: month, 2: day, 3: hour, 4: minutes, 5: seconds, 6: year). I don't think "00" for the seconds is valid.
  • The first argument of time.Parse describes the layout of the date in the string. "01/02/2006 15:04:05" tells that function that the day and month have leading zeros.
neon harbor
#

Regarding the return part, if Parse returns two values and your function is only meant to return one, don't use return there. Assign the values to variables first, do whatever you need to do and return the value you need to return.

raven laurel
#

He accidentally opened two threads, there is another one im in

neon harbor
#

Woops. I'll close this one.