I am searching for ways to populate a Go template with sql values. So far I have 3 solutions that has pros and cons. However the date displays in a funny way.
From PGAdmin the date displays correct 2020-08-26
Using sql standard library the date displays 2020-08-26 00:00:00 +0000 +0000
Using sqlx the date displays 2020-08-26T00:00:00Z
Using json the date displays correct 2020-08-26
Or is there no date format in Golang Struct?
1. Sql standard library
Standard sql library is idiomatic, but you have to repeat the structs twice.
Sqlx is an add on to standard library with “StructScan” which makes it more generic. I interpret this is still an idiomatic way.
3. JSON experiment
This is DRY at the highest level, but I am considering it as a dead end. Because the function json_agg() does not work with joins etc. But I still consider it is an option for some cases.
- How do I get rid of the time garbage (“T00:00:00Z”) in 1 and 2? Is there a way to use time.Date(YYYY-MM-DD) in a struct sort of?
- Is there more ways to achieve this? Simpler or better?