Trying to use age() function from postgresql with DateOfBirth
type Person struct {
ID int
DOB time.Time
}
query := `SELECT id, AGE(dateofbirth) from person WHERE id = $1`
But got an error
Unsupported Scan, storing driver.Value type []uint8 into type *time.Time
The age() function work if I just pass the DOB as string
type Person struct {
ID int
DOB string
}
But I want to check the validity of DOB so I use time.Parse("2006-01-02", DOB)
func createPerson(w http.ResponseWriter, r *http.Request) {
err := r.ParseForm()
if err != nil {
log.Fatal(err)
}
dob := r.PostForm.Get("dob")
birthday, _ := time.Parse("2006-01-02", dob)
err = models.person.Insert(birthday)
if err != nil {
log.Fatal("Server Error: ", err)
return
}
http.Redirect(w, r, "/", http.StatusSeeOther)
}
func getPerson(w http.ResponseWriter, r *http.Request) {
id, err := strconv.Atoi(chi.URLParam(r, "id"))
if err != nil {
log.Fatal("Not found", err)
return
}
person, err = models.person.Get(id)
if err != nil {
log.Fatal("Server Error: ", err)
return
}
render.HTML(w, http.StatusOK, "person.html", person)
}
dob := r.PostForm.Get("dob")
and validate then parsebirthday, _ := time.Parse("2006-01-02", dob)
andmodels.person.Insert(birthday)
Then,person, err := models.person.Get(id)