Search code examples
htmlstringgounicodeencode

How to convert a string from unicode to html entity


I'm trying to convert an Arabic string like this:

ص

to this:

ص

Is there a way (or package) to do it in Go?


Solution

  • That character is not special in HTML, so you can include it as-is in the output, just be sure to set the proper encoding of the document.

    Note that to escape special characters in strings, you may use html.EscapeString(). But because ص is not special in HTML, that will not change.

    If for some reason you do need to escape it, you may simply use the decimal representation of the rune:

    fmt.Println(html.EscapeString("ص"))
    fmt.Printf("&#%d;", 'ص')
    

    Outputs (try it on the Go Playground):

    ص
    ص