I should probably explain why would I want that first.
I understand in Go substring(s[i:j]) and string.Split and some other string operations work in-place: the resulting substrings share the same memory block of the original string.
For example I read a large string, parse and get a few substrings from it, which will be kept in the long run in a server program, they will "hold" the large memory block from GC, wasting memory. I assume if I could make a copy of those substrings and keep those copies instead, GC could free that large string.
But I can't find a string copy mechanism in Go, I tried converting it to []byte then string again, memory usage dropped roughly 3/4 in my particular use case.
But this doesn't feel right: 1st, it introduces two copy operations. 2nd, since I never really write to that byte slice, I suspect it might got optimized out in release builds.
I can't imagine this hasn't been asked before, but my search doesn't yield any relevant results, or is there some better practices to do these kinds of things in Go?
BTW I tried to append an empty string(+"") to it, memory consumption doesn't drop, I assume it got optimized out even in test builds.
For measuring memory usage, I call runtime.GC() then runtime.ReadMemStats() and compare MemStats.Alloc, which seems pretty consistent in my tests.