M
M
Michail Wowtschuk2017-07-09 20:31:38
go
Michail Wowtschuk, 2017-07-09 20:31:38

How to pass special characters to a template in golang?

It is necessary to transfer such character & # 9654; in templates, how can this be done?
Spaces made the character not appear in the question.
When compiling, it swears at #

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Pavlyuk, 2017-07-10
@wowtschuk

Just enclose the character itself in quotes, not ▶, but the character itself.

datas = Data{
    "▶",
}

Plus, you have a lowercase symbol field, which means it's private, so it won't be accessible from the template. We need to name it Symbol.
https://play.golang.org/p/6t3UI9OFbY
package main

import (
  "bytes"
  "fmt"
  "html/template"
)

func main() {
  t, err := template.New("index").Parse("Symbol: {{.play.Symbol}}")
  if err != nil {
    panic(err)
  }

  type Data struct{ Symbol string }

  var out_data = make(map[string]Data)

  datas := Data{
    "▶",
  }

  out_data["play"] = datas

  b := bytes.NewBuffer([]byte{})
  err = t.ExecuteTemplate(b, "index", out_data)
  if err != nil {
    panic(err)
  }

  fmt.Println(string(b.Bytes()))
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question