#Go track - help working with time.Parse

14 messages · Page 1 of 1 (latest)

lucid chasm
#

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.

lucid chasm
#

I've come up with this, but is this really an acceptable way to do it?

// Schedule returns a time.Time from a string containing a date.
func Schedule(date string) time.Time {
    // Parse given date assuming format M/Y/YYYY H:i:s
    time, err := time.Parse("1/2/2006 15:04:05", date)

    // Panic if error parsing date.
    if err != nil {
        panic(err)
    }

    return time
}
high garnet
#

What did the test want? Did you read the example from the go doc page? @lucid chasm

#

Also please next time don't open 2 threads at once for the same topic

high garnet
#

@lucid chasm Hello, did you solve it?

lucid chasm
lucid chasm
high garnet
#

if there's a way to ignore the second return value from time.Parse and just care about the time.Time object

yes there is. that's why i asked if you have read the doc

split hound
#

If it passes the tests, then it does what it needs doing 😁
If your code passes all the tests and you want further feedback, we recommend you use the website to request a code review.

lucid chasm
#

I see. Thanks!

To be honest, I was having a bit of a meltdown over the whole way Go uses reference date and American date formats, so I asked for help here to try to reduce the likelihood of me throwing my laptop out the window 😭 (I've always had a hard time getting my head around date/time as it is, let alone thinking about region-specific stuff, lol)

I'll refer the docs and if I'm still not sure I'll check out the code review feature. Ta.

split hound
#

Go is certainly ... unique in their approach

lucid chasm
#

I'm from a background of PHP which is certainly more forgiving. Thanks for your help.

lucid chasm
#

So, I figured it out which is nice. 🙂 Thanks again