func newpost(res http.ResponseWriter, req *http.Request) { header := req.PostFormValue("head") content := req.PostFormValue("content") var values []interface{} values = append(values, header) values = append(values, content) //store into db rows := db.Query("insert into posts(header, content, date) VALUES($1, $2, now())", values) db.DeferRows(rows) http.Redirect(res, req, "/", 301) }
//get all the posts from the db and return it as an struct func GetPosts() SQLRows { var values []interface{} var sqlRows SQLRows var sqlRow SQLRow rows := db.Query("SELECT * FROM posts ORDER BY id DESC", values) for rows.Next() { var id int var header, content string var sqlDate time.Time rows.Scan(&id, &header, &content, &sqlDate) sqlRow.Header = header sqlRow.Content = content sqlRow.Date = sqlDate.String() sqlRows.SQLRows = append(sqlRows.SQLRows, sqlRow) } db.DeferRows(rows) return sqlRows }