Golang html/template.HTMLEscape function example

package html/template

HTMLEscape writes to w(1st parameter) the escaped HTML equivalent of the plain text data b(2nd parameter).

Golang html/template.HTMLEscape function usage example

 package main

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

 func main() {

 w := bytes.NewBufferString("")

 b := []byte("<script>alert('xss attack!')</script>")

 template.HTMLEscape(w, b)

 fmt.Println(w)

 }

Output :

&lt;script&gt;alert(&#39;xss attack!&#39;)&lt;/script&gt;

Reference :

http://golang.org/pkg/html/template/#HTMLEscape

Advertisement