When to use []byte or string in Go?

后端 未结 3 1951
眼角桃花
眼角桃花 2021-01-31 01:05

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

3条回答
  •  不思量自难忘°
    2021-01-31 02:12

    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.

提交回复
热议问题