1

How to convert string text to utf8 in golang (go language)?

shagul
  • 509
  • 2
  • 7
  • 11

1 Answers1

0

The string literals in go are encoded in UTF-8 by default. From the official golang blog

Go source code is UTF-8, so the source code for the string literal is UTF-8 text

However this doesn't apply to the escape characters, such as \n:

If that string literal contains no escape sequences, which a raw string cannot, the constructed string will hold exactly the source text between the quotes.

Hope this helps!

  • i'm sorry ,i meant it in wrongly ,actually i want to encode json data in UTF-8 format – shagul Oct 17 '16 at 10:06
  • Then better post another question – Jakubowsky Oct 17 '16 at 10:11
  • @shagul use the [Unmarshal](https://golang.org/pkg/encoding/json/#Unmarshal/) function, store the content in a struct, and all the strings will be encoded to utf8, except the invalid characters will be replaced by `U+FFFD`, and change all other datatypes to `String` with the [strconv](https://golang.org/pkg/strconv/) package. – Jakubowsky Oct 17 '16 at 10:26