Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replacing HTML entities in Go strings

Tags:

html

go

This feels like one of those operations that must be common enough that this is a duplicate question, but since I was not able to find it anywhere:

What is the most straightforward way to replace HTML entities with their string representations in a Go string? That is, how would I turn the string Rø&d grød & fløde into Rød grød & fløde?

My own solutions have been either using strings.Replace on all relevant entities (which quickly becomes intractable), or wrapping the string into an XML document and decoding it with xml.Decoder (which seems silly and leads to numerous edge cases).

like image 434
fuglede Avatar asked Nov 13 '17 18:11

fuglede


1 Answers

Use the html.UnescapeString function:

fmt.Println(html.UnescapeString("Rø&d grød & fløde"))

playground example

like image 121
Bayta Darell Avatar answered Oct 31 '22 06:10

Bayta Darell