"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 Efficiently Convert UTF-8 Strings to Byte Arrays in Go?

How to Efficiently Convert UTF-8 Strings to Byte Arrays in Go?

Published on 2024-11-08
Browse:694

How to Efficiently Convert UTF-8 Strings to Byte Arrays in Go?

Converting UTF-8 Strings to Byte Arrays

Unmarshalling JSON requires a byte slice input, while strings are stored as UTF-8 in Go. This article explores the efficient conversion of UTF-8 strings to byte arrays.

Direct Conversion

Go allows converting strings to byte slices, creating a copy of the string's bytes:

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

However, for large strings, this approach is inefficient due to the copying.

Using io.Reader

An efficient alternative is using strings.NewReader() to create an io.Reader that reads from the string without copying:

s := `{ "somekey": "somevalue" }`

var result interface{}
err := json.NewDecoder(strings.NewReader(s)).Decode(&result)

This approach avoids copying the string.

Small JSON Texts

For small JSON texts, direct conversion remains a fast option:

s := `{ "somekey": "somevalue" }`

var result interface{}
err := json.Unmarshal([]byte(s), &result)

Note: When reading JSON from an io.Reader (e.g., file or network), pass the io.Reader directly to json.NewDecoder() without intermediate string reads.

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