And if you use byte[] everywhere rather than string be prepared for lots of panics and hard to detect bugs. :)
> On Jan 29, 2021, at 8:35 AM, Amnon <amno...@gmail.com> wrote: > > Generally yes. > As strings are immutable, and []byte is not, > if you convert from string -> []byte, you need a new copy which can be > written without changing the original string (which may be referred to > elsewhere). > > If you convert from []byte -> string, then you need a new copy so that any > later changes to the byte slice won't change the contents of the string. > > There are a few specialised situations (like conversion from []byte -> string > inside a map index where these copies optimised away. > But otherwise you are stuck with an allocation. > > The fastest way to convert between string and []byte is not to convert > between string and []byte, (i.e. just use []byte everywhere). > > >> On Friday, 29 January 2021 at 12:55:07 UTC cuiw...@gmail.com wrote: >> I mean convert using >> s := "abcefg" >> b := []byte(s) >> s2 := string(b) >> >> not convert using some unsafe.Pointer trick. >> >>> On Friday, January 29, 2021 at 8:51:57 PM UTC+8 xie cui wrote: >>> does convert string to []byte, and convert []byte to string alway alloc new >>> space, and the new space is in heap? >>> if it is not, please show some demo codes? > > -- > You received this message because you are subscribed to the Google Groups > "golang-nuts" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to golang-nuts+unsubscr...@googlegroups.com. > To view this discussion on the web visit > https://groups.google.com/d/msgid/golang-nuts/910b30e3-367d-4fe8-9932-172c13069effn%40googlegroups.com. -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/CCE2E43D-8A69-4AEF-B538-E3D2C304C7D9%40ix.netcom.com.