Go: display array in array with templates -
how insert variable in go template - have code in html:
{{define "homepage"}} <html> <form action="/home/delete" method="post"> {{with .posts}} {{range .}} <p id="twt">{{ range $i := .status}}{{$i}}<br><br></p> <button type="submit" id="xbutton" name="xdel" value="{{.tweetid}}">delete</button> {{end}} {{end}} {{end}} </form> </html> {{end}}
the code in go:
type user struct { userid int64 username string password string posts []*post } type post struct { tweetid int username string status []string } func deletehandler(w http.responsewriter, r *http.request) { currentuser = getusername(r) postvalue = r.postformvalue("xdel") deletetweet() if currentuser != "" { := post{username: currentuser, status: readstatus(), tweetid: readstatusid()} person := user{username: currentuser, posts: []*post{&as}} t := template.must(template.new("tele").parsefiles("layout/home.html")) if err := t.executetemplate(w, "homepage", person); err != nil { panic(err) } } else { http.redirect(w, r, "/", 302) } } //getting .tweetid func readstatusid() (res int) { //some code open , access sql database rows, _ := db.query("select id posts tweet = ?", addtweet) code error handling defer rows.close() var status string rows.next() { err := rows.scan(&status) if err != nil { fmt.println(err) } fmt.printf("this %s", status) } return } func main() { (//code other handlers..) router.handlefunc("/home/delete", deletehandler).methods("post") }
however, error message is
can't evaluate field tweetid in type string
how fix , allow .tweetid read in value string? if helps referred way i've used templates: http://jan.newmarch.name/go/template/chapter-template.html
try change template:
{{range $p := .posts}} <p id="twt">{{ range $i := .status}}{{$i}}<br><br></p> <button type="submit" id="xbutton" name="xdel" value="{{$p.tweetid}}">delete</button> {{end}} {{end}}
using {{.tweetid}}
inside {{ range $i := ... }}
reference {{$i.tweetid}}
adding range $p := .posts}}
, referencing explicitly {{$p.tweetid}}
solve problem
Comments
Post a Comment