Golang text/template.JSEscape() function example

package text/template

Golang text/template.JSEscape() function usage example

 package main

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

 func main() {

  w := bytes.NewBufferString("")

  b := []byte("<script>document.getElementById('demo').innerHTML = cars;</script>")

  template.JSEscape(w, b)

  fmt.Println(w)

 }

Output :

\x3Cscript\x3Edocument.getElementById(\'demo\').innerHTML = cars;\x3C/script\x3E

Reference :

http://golang.org/pkg/text/template/#JSEscape

Advertisement