Original post

Does anyone know if any sort of reallocation etc is done on a string if it is passed into a function, that function makes no changes to it, and returns that same string which is then put back into the original variable by the caller.

Not sure I’m explaining it well, but something like:

myStrVar = SomeFunction(myStrVar)

func SomeFunction(strParam string) string { // some tests done which determine that // no changes where need to the string return strParam }

Does myStrVar get reallocated, ending up with the string being copied around etc?

Would I end up needing to mess around with pointers to avoid reallocation if no changes to the underlying string data?

Thanks for any insights on this!

Edit – sorry, don’t know how to format the code block so it doesn’t get mangled.

submitted by /u/gatuin
[link] [comments]