Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to escape the html using golang?

Tags:

go

The html text following:

<script type="text/javascript">alert(123);</script>
<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.

like image 398
HeadwindFly Avatar asked Jun 28 '16 01:06

HeadwindFly


People also ask

What is escaping HTML?

Escaping in HTML means, that you are replacing some special characters with others. In HTML it means usally, you replace e. e.g < or > or " or & . These characters have special meanings in HTML. And the text will appear as hello, world.


1 Answers

There's EscapeString function in html package

unescaped := `<script>alert(123);</script>`
escaped := html.EscapeString(unescaped)
like image 145
jussius Avatar answered Oct 09 '22 10:10

jussius