What are the differences between these two strings?


(Jason Edgar) #1

When inserting an event in google-calendar-api using Golang I can get my event to work when I hard code the date ‘DateTime: “2019-04-11T14:00:00”,’
but not when I generated it from variables ‘DateTime: evtEndDate + “T” + evtEndTime,’
I can’t see any difference but the API does
Here I printf the constructed variable and it’s type followed by the hardcoded variable and it’s type. They look identical

2019-04-11T06:00:00     string
2019-04-11T06:00:00     string
2019-04-11T14:00:00     string
2019-04-11T14:00:00     string
2019/04/11 12:48:17 Unable to create event. googleapi: Error 400: Invalid value for: Invalid format: "2019-04-11T14:00:00", invalid
exit status 1

part of the go code I’m using is:

event := &calendar.Event{
    Summary:     data.schShift + " - Test Event",
    Location:    "Canada",
    Description: "Test event added by Go utility",
    Start: &calendar.EventDateTime{

        // This doesn't work
        DateTime: evtStartDate + "T" + evtStartTime,

        // This works (when unREM'd
        // DateTime: "2019-04-11T06:00:00",
        TimeZone: "America/Toronto",
    },
    End: &calendar.EventDateTime{

        // This doesn't work
        DateTime: evtEndDate + "T" + evtEndTime,

        // This works (when unREM'd
        // DateTime: "2019-04-11T14:00:00",
        TimeZone: "America/Toronto",
    },
}
evtStartDate = evtStartDate + "T" + evtStartTime
fmt.Printf("%v\t%T\n", evtStartDate, evtStartDate)
fmt.Printf("%v\t%T\n", event.Start.DateTime, event.Start.DateTime)
evtEndDate = evtEndDate + "T" + evtEndTime
fmt.Printf("%v\t%T\n", evtEndDate, evtEndDate)
fmt.Printf("%v\t%T\n", event.End.DateTime, event.End.DateTime)

I expected the variable to be accepted as it seems to be the same type but no


(BB-8) #2

If I had to guess looking at the code provided, I’d say it’s probably not a type issue but a syntax issue for the date string, I’d advise using the time package to generate the date string.

Here’s an example: https://goplay.space/#CUxXvptQ6fD

For reference, here’s the documentation for the time package.
https://golang.org/pkg/time/

I hope this was useful.


(Jason Edgar) #3

Thanks BB-8. The text string works fine, it turned out to be a variable scope issue I believe. I did the setup outside of the struct and all went well.