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? []byteare not printed as nicely asstringby default, so I often find myself casting tostringfor 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. – Ashe May 31 '12 at 4:57[]rune, which is the best to represent a mutable string of characters – newacct May 31 '12 at 17:21rune? My opinion of Google has gone up a bit. – JAB Jun 15 '12 at 12:59