Let's convert string
to []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?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…