如何将值从MySQL传递到Go模板中

huangapple go评论135阅读模式
英文:

How to pass value from MySQL to go template

问题

这是一个用于将值传递给模板的结构体:

  1. type Entry struct {
  2. Name, Mes string
  3. }

这是一个处理HandleFunc("/",mysqlWithTempl)的函数:

  1. func mysqlWithTempl(w http.ResponseWriterr *http.Request) {
  2. // 打开数据库
  3. conerr := sql.Open("mymysql"dbName+"/"+dbUserName+"/"+dbPassword)
  4. if err != nil {
  5. panic(err)
  6. }
  7. // 关闭数据库
  8. defer con.Close()
  9. // 查询
  10. rowserr := con.Query("select name,message from entry")
  11. tRes := Entry{}
  12. // 获取结果
  13. for rows.Next() {
  14. var namemessage string
  15. rows.Scan(&name,&message)
  16. tRes.Name = name
  17. tRes.Mes = message
  18. }
  19. index.Execute(wtRes)
  20. }

这是一个模板:

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title> Test </title>
  5. </head>
  6. <body>
  7. <section id="contents">
  8. <p> {{.Mes}} {{.Name}} </p>
  9. </section>
  10. </body>
  11. </html>

我的问题是如何从mysql中获取更多的值并将其传递给模板。

当尝试以下代码时:

  1. for rows.Next() {
  2. var namemessage string
  3. rows.Scan(&name,&message)
  4. tRes.Name = name
  5. tRes.Mes = message
  6. index.Execute(wtRes)
  7. }

所有结果都被获取,但每个结果都被重复显示。

英文:

This struct to pass value to template

  1. type Entry struct {
  2. Name, Mes string
  3. }

This function to handle with HandleFunc(&quot;/&quot;, mysqlWithTempl)

  1. func mysqlWithTempl(w http.ResponseWriter, r *http.Request) {
  2. // Open database
  3. con, err := sql.Open(&quot;mymysql&quot;, dbName+&quot;/&quot;+dbUserName+&quot;/&quot;+dbPassword)
  4. if err != nil {
  5. panic(err)
  6. }
  7. // Close database
  8. defer con.Close()
  9. //query
  10. rows, err := con.Query(&quot;select name, message from entry&quot;)
  11. tRes := Entry{}
  12. //fetch result
  13. for rows.Next() {
  14. var name, message string
  15. rows.Scan(&amp;name, &amp;message)
  16. tRes.Name = name
  17. tRes.Mes = message
  18. }
  19. index.Execute(w, tRes)
  20. }

and this template

  1. &lt;!DOCTYPE html&gt;
  2. &lt;html&gt;
  3. &lt;head&gt;
  4. &lt;title&gt; Test &lt;/title&gt;
  5. &lt;/head&gt;
  6. &lt;body&gt;
  7. &lt;section id=&quot;contents&quot;&gt;
  8. &lt;p&gt; {{.Mes}} {{.Name}} &lt;/p&gt;
  9. &lt;/section&gt;
  10. &lt;/body&gt;
  11. &lt;/html&gt;

my ask how can fetch more value from mysql and pass it to template

When try

  1. for rows.Next() {
  2. var name, message string
  3. rows.Scan(&amp;name, &amp;message)
  4. tRes.Name = name
  5. tRes.Mes = message
  6. index.Execute(w, tRes)
  7. }

all result fetched but every thing repeated

答案1

得分: 5

我认为你可能在问如何显示多个结果?

如果是这样,那么你想要将所有的行累积到一个切片中,代码如下:

  1. results := []Entry
  2. for rows.Next() {
  3. var name, message string
  4. rows.Scan(&name, &message)
  5. tRes.Name = name
  6. tRes.Mes = message
  7. results = append(results, tRes)
  8. }
  9. index.Execute(w, results)

然后你的模板将会改变如下:

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title> Test </title>
  5. </head>
  6. <body>
  7. <section id="contents">
  8. {{range .}}
  9. <p> {{.Mes}} {{.Name}} </p>
  10. {{end}}
  11. </section>
  12. </body>
  13. </html>

我相信这将导致每行返回的mysql查询结果运行一次<p> {{.Mess}} {{.Name}} </p>

不过我还没有测试过这段代码,所以可能会有错误。

英文:

I think you might be asking how to show multiple results?

If so then you want to accumulate all your rows into a slice like so:

  1. results := []Entry
  2. for rows.Next() {
  3. var name, message string
  4. rows.Scan(&amp;name, &amp;message)
  5. tRes.Name = name
  6. tRes.Mes = message
  7. results = append(results, tRes)
  8. }
  9. index.Execute(w, results)

Then your template will change to look like this:

  1. &lt;!DOCTYPE html&gt;
  2. &lt;html&gt;
  3. &lt;head&gt;
  4. &lt;title&gt; Test &lt;/title&gt;
  5. &lt;/head&gt;
  6. &lt;body&gt;
  7. &lt;section id=&quot;contents&quot;&gt;
  8. {{range .}}
  9. &lt;p&gt; {{.Mes}} {{.Name}} &lt;/p&gt;
  10. {{end}}
  11. &lt;/section&gt;
  12. &lt;/body&gt;
  13. &lt;/html&gt;

I believe this will cause one &lt;p&gt; {{.Mess}} {{.Name}} &lt;/p&gt; to be run per row that your mysql query returns.

I haven't actually tested this code though so it might have errors.

huangapple
  • 本文由 发表于 2012年9月11日 01:23:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/12356584.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定