fuglede
fuglede

Reputation: 18221

Replacing HTML entities in Go strings

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).

Upvotes: 15

Views: 8621

Answers (1)

Thundercat
Thundercat

Reputation: 121099

Use the html.UnescapeString function:

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

playground example

Upvotes: 25

Related Questions