html.EscapeString and html.UnescapeString
func EscapeString(s string) string
func UnescapeString(s string) stringEscapeString encodes <, >, &, ' and ". UnescapeString decodes a wider set of entities back to Unicode characters.
| Parameter | Description |
|---|---|
s | The string to escape or unescape. |
Returns: A new string. The original value is not modified.
- UnescapeString("á") returns á, which EscapeString would never produce from that letter.
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(`<p class="note">A & B</p>`))
fmt.Println(html.UnescapeString("á"))
}Expected output
<p class="note">A & B</p>
it's ok
<p class="note">A & B</p>
á
Related: HTML escape tool, html/template, Official html