为了账号安全,请及时绑定邮箱和手机立即绑定

如何打印到 HTML 模板

如何打印到 HTML 模板

Go
互换的青春 2022-05-05 15:53:46
如何在 Go 中打印到 HTML 模板? 来源:https ://github.com/timpalpant/go-iex我必须在 go 代码和 html 模板中遗漏很多东西。我正在寻找一种通过将值打印到 HTML 模板来解决问题的方法:package mainimport (  "fmt"  "html/template"  "net/http"  "github.com/timpalpant/go-iex")func process(w http.ResponseWriter, r *http.Request) {  client := iex.NewClient(&http.Client{})  quotes, err := client.GetTOPS([]string{"AAPL", "SPY"})  if err != nil {    panic(err)  }  var s string  for _, quote := range quotes {    s := fmt.Sprintf("%v: bid $%.02f (%v shares), ask $%.02f (%v shares) [as of %v]\n",      quote.Symbol, quote.BidPrice, quote.BidSize,      quote.AskPrice, quote.AskSize, quote.LastUpdated)  }  t, _ := template.ParseFiles("test.html")  quote := s  t.Execute(w, quote)}func main() {  server := http.Server{    Addr: "127.0.0.1:8080",  }  http.HandleFunc("/process", process)  server.ListenAndServe()}这是 HMTL 模板<!DOCTYPE html><html>  <head>    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">    <title>Go Web Programming</title>  </head>  <body>    <ul>    {{ range . }}      <li>{{ . }}</li>    {{ end}}    </ul>  </body></html>错误:./web.go:22:5: 已声明但未使用
查看完整描述

2 回答

?
婷婷同学_

TA贡献1844条经验 获得超8个赞

在循环内部,您通过 usingfor重新声明s为新的循环范围变量s :=,如下面的代码所示。您永远不会在其范围内使用该特定变量,因此会出现错误:s declared and not used.


// this instance of s is scoped to the function

var s string

for _, quote := range quotes {

  // *** this instance of s is only scoped to loop and it's not actually used within loop ***//

  s := fmt.Sprintf("%v: bid $%.02f (%v shares), ask $%.02f (%v shares) [as of %v]\n",

    quote.Symbol, quote.BidPrice, quote.BidSize,

    quote.AskPrice, quote.AskSize, quote.LastUpdated)

}

相反,s :=改为 this s =,如下所示。但是,您还没有完成,请在下面进一步阅读。


var s string

for _, quote := range quotes {

  s = fmt.Sprintf("%v: bid $%.02f (%v shares), ask $%.02f (%v shares) [as of %v]\n",

    quote.Symbol, quote.BidPrice, quote.BidSize,

    quote.AskPrice, quote.AskSize, quote.LastUpdated)

}

使用上面的代码将覆盖s循环的每次迭代的值。如果您想保留循环中生成的每个字符串并将它们全部打印到模板中,请创建一个字符串切片并附加到它...


// Create s as a slice of string

var s []string

for _, quote := range quotes {

  // append each result from loop to slice

  s = append(s, fmt.Sprintf("%v: bid $%.02f (%v shares), ask $%.02f (%v shares) [as of %v]\n",

    quote.Symbol, quote.BidPrice, quote.BidSize,

    quote.AskPrice, quote.AskSize, quote.LastUpdated)) 

}

这是 html 模板中的输出,它现在包括s切片中的所有值...

//img1.sycdn.imooc.com//627382b00001605006970131.jpg

查看完整回答
反对 回复 2022-05-05
?
慕的地8271018

TA贡献1796条经验 获得超4个赞

您正在重新定义流程函数中的 s 。


  var s string

  for _, quote := range quotes {

    s = fmt.Sprintf("%v: bid $%.02f (%v shares), ask $%.02f (%v shares) [as of %v]\n",

      quote.Symbol, quote.BidPrice, quote.BidSize,

      quote.AskPrice, quote.AskSize, quote.LastUpdated)

   // ****************REMOVE s:= from here.**********

  }

例子:


func main() {

    s := ""

    items := []int{1}

    for _, quote := range items {

        s = "1"

        fmt.Println(quote)

    }

    fmt.Println(s)

}


查看完整回答
反对 回复 2022-05-05
  • 2 回答
  • 0 关注
  • 110 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号