该结构将值传递给模板type Entry struct { Name, Mes string}此功能要处理 HandleFunc("/", mysqlWithTempl)func mysqlWithTempl(w http.ResponseWriter, r *http.Request) { // Open database con, err := sql.Open("mymysql", dbName+"/"+dbUserName+"/"+dbPassword) if err != nil { panic(err) } // Close database defer con.Close() //query rows, err := con.Query("select name, message from entry") tRes := Entry{} //fetch result for rows.Next() { var name, message string rows.Scan(&name, &message) tRes.Name = name tRes.Mes = message } index.Execute(w, tRes)}和这个模板<!DOCTYPE html><html> <head> <title> Test </title> </head> <body> <section id="contents"> <p> {{.Mes}} {{.Name}} </p> </section> </body></html>我问如何从mysql获取更多值并将其传递给模板当尝试 for rows.Next() { var name, message string rows.Scan(&name, &message) tRes.Name = name tRes.Mes = messageindex.Execute(w, tRes) }取得所有结果,但每件事重复
1 回答
- 1 回答
- 0 关注
- 143 浏览
添加回答
举报
0/150
提交
取消