Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When to use []byte or string in Go?

Frequently in writing Go applications, I find myself with the choice to use []byte or string. Apart from the obvious mutability of []byte, how do I decide which one to use?

I have several use cases for examples:

  1. A function returns a new []byte. Since the slice capacity is fixed, what reason is there to not return a string?
  2. []byte are not printed as nicely as string by default, so I often find myself casting to string for logging purposes. Should it always have been a string?
  3. When prepending []byte, a new underlying array is always created. If the data to prepend is constant, why should this not be a string?
like image 578
Matt Joiner Avatar asked May 31 '12 02:05

Matt Joiner


People also ask

Is byte [] same as string?

Byte objects are sequence of Bytes, whereas Strings are sequence of characters. Byte objects are in machine readable form internally, Strings are only in human readable form. Since Byte objects are machine readable, they can be directly stored on the disk.

What is [] rune GoLang?

What is GoLang rune? A character is defined using “code points” in Unicode. Go language introduced a new term for this code point called rune. Go rune is also an alias of type int32 because Go uses UTF-8 encoding.

What is a byte string in GoLang?

Byte. A byte in Go is an unsigned 8-bit integer. That means it has a limit of 0–255 in the numerical range. type byte = uint8. According to Go documentation, Byte is an alias for uint8 and is the same as uint8 in all ways.

How many bytes is a string in GoLang?

Hence in Go, all characters are represented in int32 (size of 4 bytes) data type.


2 Answers

My advice would be to use string by default when you're working with text. But use []byte instead if one of the following conditions applies:

  • The mutability of a []byte will significantly reduce the number of allocations needed.

  • You are dealing with an API that uses []byte, and avoiding a conversion to string will simplify your code.

like image 53
andybalholm Avatar answered Sep 29 '22 03:09

andybalholm


I've gotten the sense that in Go, more than in any other non-ML style language, the type is used to convey meaning and intended use. So, the best way to figure out which type to use is to ask yourself what the data is.

A string represents text. Just text. The encoding is not something you have to worry about and all operations work on a character by character basis, regardless of that a 'character' actually is.

An array represents either binary data or a specific encoding of that data. []byte means that the data is either just a byte stream or a stream of single byte characters. []int16 represents an integer stream or a stream of two byte characters.

Given that fact that pretty much everything that deals with bytes also has functions to deal with strings and vice versa, I would suggest that instead of asking what you need to do with the data, you ask what that data represents. And then optimize things once you figure out bottlenecks.

EDIT: This post is where I got the rationale for using type conversion to break up the string.

like image 28
Abhay Buch Avatar answered Sep 29 '22 02:09

Abhay Buch