Sometimes, I want to create a new string which will not share the underlying bytes with other strings.
For example, to avoid the substring memory leak problem.
newStr := string([]byte(oldStr)) is simple but will make two memory allocations, and one of them is a waste.
There are two solutions which only make one memory allocation in
https://groups.google.com/forum/#!topic/golang-nuts/naMCI9Jt6Qg(code:
https://play.golang.org/p/3rVsMTaHUz).
But in that thread, someone said the two are not safe from being optimized unexpectedly in later compiler versions.
I found the most approached function in the standard lib is strings.Repeat(), but it also makes two memory allocations.
Is there a safe way to do this with only one memory allocation?