"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > How to Convert UTF-8 Strings to []byte for JSON Unmarshalling in Go?

How to Convert UTF-8 Strings to []byte for JSON Unmarshalling in Go?

Published on 2024-11-08
Browse:134

How to Convert UTF-8 Strings to []byte for JSON Unmarshalling in Go?

Unmarshalling UTF-8 Strings to []byte

When working with JSON, the unmarshal function requires an input of type []byte. However, our data could be stored as a UTF-8 string. This article explores how to convert a UTF-8 string to []byte for successful unmarshalling.

Conversion Using []byte(s)

According to the Go specification, a string can be converted to []byte using a simple casting:

s := "some text"
b := []byte(s)

However, this conversion creates a copy of the string's content, which can be inefficient for large strings.

Using io.Reader for Efficient Unmarshal

An alternative approach is to use an io.Reader created from the string:

s := `{"somekey":"somevalue"}`
reader := strings.NewReader(s)
decoder := json.NewDecoder(reader)
var result interface{}
decoder.Decode(&result)

This method avoids copying the string and is more efficient for large inputs.

Considerations for Different Scenarios

  • For small JSON texts, directly converting to []byte using []byte(s) is acceptable.
  • For large JSON texts or when working with io.Readers, using strings.NewReader and json.NewDecoder provides better efficiency.

In summary, converting UTF-8 strings to []byte for unmarshalling involves either direct casting or using an io.Reader for efficient handling of large inputs. The choice depends on the specific requirements of the application.

Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3