What is the use of using a byte packet over strings?

http://play.golang.org/p/CZTmhNepkM

Can someone explain why we need bytes? For example, in a playgound game, I do the same, but a packet of strings is easier than using bytes. Why would anyone need a byte package for HasSuffix functions while we have a string package?

 strings.HasSuffix(word, "ed")
 bytes.HasSuffix(word_byte, []byte("ed"))
+4
source share
3 answers

Somewhat earlier than Go v1.0, there was no type rune. Therefore, some methods in the standard library that work with parts of strings or sort out "characters" occupy a byte slice (instead of cutting a rune).

- .

+2

. , ( Go ), API- .

+3

Go 1.8 , ASCII.

. commit 9a8c695 (dsnet)

> bytes, strings: ASCII

Google :

ContainsAny|IndexAny|LastIndexAny|Trim|TrimLeft|TrimRight

, 97% , ASCII.

Uses of ContainsAny|IndexAny|LastIndexAny:
     6% are 1   character  (e.g., "\n" or " ")
    58% are 2-4 characters (e.g., "<>" or "\r\n\t ")
    24% are 5-9 characters (e.g., "()[]*^$")
    10% are 10+ characters (e.g., "+-=&|><!(){}[]^\"~*?:\\/ ")

We optimize for ASCII sets, which are usually used to search for "control" characters in some string. We do not optimize for a single-character scenario, as IndexRuneor IndexBytecan be used

0
source

All Articles