Im trying to get the values of the database by this way. But the compiler throw me an error when I go to /myapps direction.
Structure:
type App struct{
Title string
Author string
Description string
}
Function:
func myappsHandler(w http.ResponseWriter, r *http.Request){
db, err := sql.Open("postgres"," user=postgres dbname=lesson4 host=localhost password=1234 sslmode=disable")
if err != nil{
log.Fatal(err)
}
rows, err := db.Query(`SELECT title, author, description FROM apps
WHERE title ILIKE $1
OR author ILIKE $1
OR description ILIKE $1`)
defer rows.Close()
if err != nil{
log.Fatal(err)
}
apps := []App{}
for rows.Next(){
b := App{}
err := rows.Scan(&b.Title, &b.Author, &b.Description)
if err != nil{
log.Fatal(err)
}
apps = append(apps, b)
}
render(w, "myapps.html", map[string]interface{}{"apps" : apps})
db.Close()
}
I read like this in the HTML:
{{ range .apps}}
<tr>
<td>{{ .Title }}</td>
<td>{{ .Author }}</td>
<td>{{ .Description }}</td>
<td> <form action="/delete">
<p class="navbar-form navbar-right"><button type="submit" class="btn btn-danger">Borrar</button> </p>
</form></td>
</tr>
{{ end }}
The error is : there is no parameter $1.
Thank you in advance.