如何在Golang中将模板输出写入文件?

I use the following code which work ok, but now I want to print the template to a file and tried the following but got error

package main

import (
    "html/template"
    "log"
    "os"
)

func main() {
    t := template.Must(template.New("").Parse(`{{- range .}}{{.}}:
    echo "from {{.}}"
{{end}}
`))
    t.Execute(os.Stdout, []string{"app1", "app2", "app3"})

    f, err := os.Create("./myfile")
    if err != nil {
        log.Println("create file: ", err)
        return
    }
    err = t.Execute(f, t)
    if err != nil {
        log.Print("execute: ", err)
        return
    }
    f.Close()
}

The error is:

execute: template: :1:10: executing "" at <.>: range can't iterate over {0xc00000e520 0xc00001e400 0xc0000b3000 0xc00009e0a2}

3个回答



使用数组作为第二个参数,而不是模板本身。</ p>

 包 main 

import(
“ html / template”
“ log”
“ os”

func main(){
t:= template.Must(template.New(“”)。 Parse({{-range。}} {{。}}:
echo“ from {{。}}”
{{end}}
))
t.Execute(os.Stdout,[ ] string {“ app1”,“ app2”,“ app3”})

f,错误:= os.Create(“ ./ myfile”)
如果错误!= nil {
log.Println(“ create file:“,err)
返回
}
err = t.Execute(f,[] string {” app1“,” app2“,” app3“})
如果err!= nil {
log .Print(“ execute:”,err)
返回
}
f.Close()
}
</ code> </ pre>

输出:</ p> \ n

  app1:
echo“ from app1”
app2:
echo“ from app2”
app3:
echo“ from app3”
</ code> </ pre>

myfile </ code>的内容为</ p>

  app1:
echo“ from app1”
app2:
回显“来自app2”
app3:
回显“来自app3”
</ code> </ pre>
</ div>

展开原文

原文

Use the array as the second argument, not the template itself.

package main

import (
        "html/template"
        "log"
        "os"
)

func main() {
        t := template.Must(template.New("").Parse(`{{- range .}}{{.}}:
        echo "from {{.}}"
{{end}}
`))
        t.Execute(os.Stdout, []string{"app1", "app2", "app3"})

        f, err := os.Create("./myfile")
        if err != nil {
                log.Println("create file: ", err)
                return
        }
        err = t.Execute(f, []string{"app1", "app2", "app3"})
        if err != nil {
                log.Print("execute: ", err)
                return
        }
        f.Close()
}

Output:

app1:
    echo "from app1"
app2:
    echo "from app2"
app3:
    echo "from app3"

And the content of myfile is,

app1:
    echo "from app1"
app2:
    echo "from app2"
app3:
    echo "from app3"



您第二次传递给模板执行的参数应该与您第一次传递的参数匹配。</ p>
\ n

首先要做的是:</ p>

  t.Execute(os.Stdout,[] string {“ app1”,“ app2”,“ app3”})
< / code> </ pre>

第二步:</ p>

  err = t.Execute(f,t)
</ code> </ pre>

您已传递模板本身( t </ code>)。 更改为:</ p>

  err = t.Execute(f,[] string {“ app1”,“ app2”,“ app3”})
</ code> < / pre>

您的模板遍历传递的参数(使用 {{range}} </ code>操作),该参数在您传递切片时有效,而在传递切片时则无效 模板,它是指向结构的指针,不是模板引擎可以迭代的对象。</ p>
</ div>

展开原文

原文

The parameter you pass to the template execution the second time should match what you pass for the first time.

First you do:

t.Execute(os.Stdout, []string{"app1", "app2", "app3"})

Second you do:

err = t.Execute(f, t)

You passed the template itself (t). Change it to:

err = t.Execute(f, []string{"app1", "app2", "app3"})

Your template iterates over the passed param (with a {{range}} action) which works when you pass a slice, and it won't work when passing the template, it's a pointer to a struct, it's not something the template engine could iterate over.



您输入了错误的参数:</ p>

  err = t.Execute(f,  t)
</ code> </ pre>

应该是</ p>

  err = t.Execute(f,[] string {“ app1  “,” app2“,” app3“})
</ code> </ pre>
</ div>

展开原文

原文

You give a wrong parameter:

err = t.Execute(f, t)

It should be

err = t.Execute(f,[]string{"app1", "app2", "app3"})

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问