下面一段小代码:
package main
import (
"os"
"text/template"
)
func main() {
name := "China"
tmpl, err := template.New("test").Parse("code element{{Status["BEIJING"]}}, {{.}}")
if err != nil {
panic(err)
}
err = tmpl.Execute(os.Stdout, name)
if err != nil {
panic(err)
}
}
目的是打印出来:code element{{Status["BEIJING"]}}, China
代码里面code element{{Status["BEIJING"]}}是字符串本身的内容,可是这么写总是报错('[' command error),请大牛指点一下!
谢谢!
```
package main
import (
"os"
"text/template"
)
func main() {
name := "China"
tmpl, err := template.New("test").Parse(`code element{{"{{"}}Status["BEIJING"]{{"}}"}}, {{.}}`)
if err != nil {
panic(err)
}
err = tmpl.Execute(os.Stdout, name)
if err != nil {
panic(err)
}
}
```
```
package main
import (
"os"
"text/template"
)
func main() {
name := "China"
tmpl, err := template.New("test").Delims("<<", ">>").Parse(`code element{{Status["BEIJING"]}}, <<.>>`)
if err != nil {
panic(err)
}
err = tmpl.Execute(os.Stdout, name)
if err != nil {
panic(err)
}
}
```
#1