https://play.golang.org/p/YXXlJlsNGa Hi, i'm not sure if compiler is smart enough to optimize v1, but likely v2 ends in separate memory.
On Tuesday, August 22, 2017 at 3:04:42 AM UTC+3, Travis Keep wrote: > > Suppose you have code like this > > > // verylongstring may be several thousand bytes long > verylongstring := GetSomeVeryLongString() > > prefix := verylongstring[:3] > > prefixStore.StorePrefixForDurationOfApplication(prefix) > > > > If I understand correctly, verylongstring cannot be garbage collected > because prefix points to the first 3 characters of it and is stored forever. > > What I want is to hold onto the first 3 characters while allowing the rest > of verylongstring to be GCed? In JAVA, you can do > > prefix := new String(verylongstring.substring(0,3)) > > which allows verylongstring to be GCed even if the application holds onto > prefix. This is because the above JAVA code has prefix reference a copy of > the first 3 characters of verylongstring instead of having prefix reference > verylongstring itself. > > What is the best way to do this in go? > > > > > > -- 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. For more options, visit https://groups.google.com/d/optout.