Skip to content

Instantly share code, notes, and snippets.

@anitsh
Last active March 25, 2020 20:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anitsh/9445b27c16bb4da90b3abe15c7b73177 to your computer and use it in GitHub Desktop.
Save anitsh/9445b27c16bb4da90b3abe15c7b73177 to your computer and use it in GitHub Desktop.
Golang data structures byte vs string comparision

Golang data structures []byte vs string comparision

Most of the projects we use both bytes as well strings data structures. I wanted to know the difference further:

A []byte is immutable and essentially just this:

type slice struct {
    data uintptr
    len int
    cap int
}

And a string is mutable and essentially just this:

type string struct {
    data uintptr
    len int
}

I was curios to know about the performance of both and found a neat project. Findings of that project: strings are faster for searches (contains, index, compare) purpose. []byte are faster in create (replace, concat) purpose.

To conclude, the comparision is like Apples and Oranges. Both have their own usecases string has been used much more widely for its ease. Decision should be made from initial analysis to choose the type of data type.

Happy Coding

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment