CaoDahua
CaoDahua

Reputation: 31

why protobuf use byte[] for bytes in golang but use string/ByteString for bytes in other language?

as specified here https://developers.google.com/protocol-buffers/docs/proto3#scalar

protobuf use byte[] for bytes in golang but use string/ByteString for bytes in other language, why the difference ?

as explained by @Volker , string is not suitalbe to hold arbitrary []byte.

then why pb is using string/ByteString for bytes in all other language except go ?

Upvotes: 1

Views: 1483

Answers (1)

Volker
Volker

Reputation: 42431

  • []byte is natural for streams of bytes
  • strings in Go are not byte streams but immutable strings
  • strings in Go should be UTF-8 encoded, at least that's what for-ranging over them assumes, using a non-UTF-8 byte stream in a string is possible but kinda inelegant.
  • there is no ByteString type in Go. Well, actually there is: []byte.

Upvotes: 1

Related Questions