Simple trick to insert a static date in Postgres using Golang sqlx

While writing some tests I tried to use a static date in a NamedExec call and got this message:

unexpected `:` while reading named param at 130

I was surprised that the pgsql driver would mistake a static value for a named parameter. Turns out that the issue lies in the way the sqlx NamedExec function parses the input SQL string before handing it off. There’s a simple work around that didn’t seem well documented. You can escape the ‘:’ character with another ‘:’ character. So to format a date to pass through cleanly this is how I format it:

dateVal := time.Now()
sqlDate := dateVal.Format("2006-01-02 15::04::05.999999999-07::00"

Hope this helps someone, or me when I inevitably forget this the next time the error crops up.