让我们将string
转换为[]byte
:
func toBytes(s string) []byte {
return []byte(s) // What happens here?
}
How expensive is this cast operation? Is copying performed? As far as I see in Go specification: Strings behave like slices of bytes but are immutable, this should involve at least copying to be sure subsequent slice operations will not modify our string s
. What happens with reverse conversation? Does []byte <-> string
conversation involve encoding/decoding, like utf8 <-> runes?