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:
- A function returns a new
[]byte
. Since the slice capacity is fixed, what reason is there to not return a string? []byte
are not printed as nicely asstring
by default, so I often find myself casting tostring
for logging purposes. Should it always have been astring
?- When prepending
[]byte
, a new underlying array is always created. If the data to prepend is constant, why should this not be astring
?
string
. If it's just opaque data being shuffled around, why not[]byte
? It comes down to the use cases. - Asherah[]rune
, which is the best to represent a mutable string of characters - newacctrune
? My opinion of Google has gone up a bit. - JAB