html

html.EscapeString and UnescapeString, including the five escaped characters, and why EscapeString is not a sanitizer for arbitrary HTML or JavaScript contexts, checked on Go 1.24.0.

The html package provides EscapeString and UnescapeString for HTML text. It is not html/template, which belongs on its own page, and it is not the HTML language tutorial.

Official documentation states that EscapeString escapes only five characters: <, >, &, ' and ". That is character escaping, not HTML parsing and not a sanitizer for arbitrary markup, attributes, or scripts. A javascript: URL contains none of those five characters, so it is left unchanged. UnescapeString(EscapeString(s)) == s always holds, but the converse is not always true, because UnescapeString understands a larger set of entities.

Examples were run against Go 1.24.0. They print strings only; no HTML is rendered and no script is executed.

Examples were checked against Go 1.24.0. See the official package documentation for the complete API.

Go reference overview →

API index

APISummary
html.EscapeString and html.UnescapeStringEscapeString encodes <, >, &, ' and ". UnescapeString decodes a wider set of entities back to Unicode characters.
EscapeString does not sanitize markup or JavaScript URLsTags are character-escaped, not removed. A javascript: URL has none of the five special characters, so it is left unchanged.

EscapeString and UnescapeString

EscapeString replaces the five special characters so the result can be placed in ordinary HTML text. In Go 1.24, a double quote becomes &#34; and a single quote becomes &#39;.

UnescapeString decodes character entities, including named entities such as &aacute; and numeric forms. It can therefore decode more than EscapeString emits.

html.EscapeString and html.UnescapeString

func EscapeString(s string) string
func UnescapeString(s string) string

EscapeString encodes <, >, &, ' and ". UnescapeString decodes a wider set of entities back to Unicode characters.

ParameterDescription
sThe string to escape or unescape.

Returns: A new string. The original value is not modified.

  • UnescapeString("&aacute;") returns á, which EscapeString would never produce from that letter.
go
package main

import (
	"fmt"
	"html"
)

func main() {
	sample := `<p class="note">A & B</p>`
	fmt.Println(html.EscapeString(sample))
	fmt.Println(html.EscapeString("it's ok"))
	fmt.Println(html.UnescapeString(`&lt;p class=&quot;note&quot;&gt;A &amp; B&lt;/p&gt;`))
	fmt.Println(html.UnescapeString("&aacute;"))
}

Expected output

&lt;p class=&#34;note&#34;&gt;A &amp; B&lt;/p&gt;
it&#39;s ok
<p class="note">A & B</p>
á

Related: HTML escape tool, html/template, Official html

Official documentation →

EscapeString is not a sanitizer

EscapeString only replaces those five characters. It does not parse HTML, remove tags, strip event-handler attributes, or classify URLs. A string with no <, >, &, ', or " is returned unchanged, even if a later consumer might treat it as a URL or as script.

Do not treat html.EscapeString as a substitute for html/template, a dedicated sanitizer, or a policy for untrusted markup. The strings below are printed as data.

EscapeString does not sanitize markup or JavaScript URLs

func EscapeString(s string) string

Tags are character-escaped, not removed. A javascript: URL has none of the five special characters, so it is left unchanged.

ParameterDescription
sThe input string. EscapeString does not parse it as HTML.

Returns: A string in which only <, >, &, ', and " have been replaced.

  • This is a character-level demonstration, not a vulnerability report and not a promise that escaped output is safe in every context.
go
package main

import (
	"fmt"
	"html"
)

func main() {
	fmt.Println(html.EscapeString("<script>alert(1)</script>"))
	fmt.Println(html.EscapeString("javascript:alert(1)"))
}

Expected output

&lt;script&gt;alert(1)&lt;/script&gt;
javascript:alert(1)

Related: html.EscapeString, html/template contextual escaping

Official documentation →