go

Go – How to escape the html using golang

go

The html text following:

&lt;script type=&#34;text/javascript&#34;&gt;alert(123);&lt;/script&gt;<script>alert(123);</script>

As mentioned above, only part of html text was escaped.

Now, I want to escape the text: <script>alert(123);</script>.

Expected result:

&lt;script type=&#34;text/javascript&#34;&gt;alert(123);&lt;/script&gt;&lt;script&gt;alert(123);&lt;/script&gt;

I need some help.

Best Solution

There's EscapeString function in html package

unescaped := `<script>alert(123);</script>`escaped := html.EscapeString(unescaped)