On Wednesday, September 5, 2018 at 10:12:51 AM UTC-4, Jan Mercl wrote: > > On Wed, Sep 5, 2018 at 4:00 PM Eric Raymond <e...@thyrsus.com > <javascript:>> wrote: > > > If I have multiple occurrences of a string constant in source code - > say, "M" - can I count on the compiler to create one static instance and > pass references to it everywhere? > > It's unspecified, so strictly speaking you cannot. (Also, everything in Go > is passed by value). However, all well known Go compilers do put text > literals into the text segment and one can reasonably expect the linker to > coalesce them. The implementation of a string variable is a two worded > struct having a pointer to the string value and the length of the string > value. Only this struct is being passed around (by value), the actual value > the struct points to never is >
Thanks, that is pretty much the exact answer I was expecting given the immutability of strings. Matters in my case because the deserialization of a repository history can contain hundreds of thousands of constants like "M", "D", "R", and "C" representing fast-export stream file operation types. I could intern them explicitly but it avoid code clutter and some lookup time if I don't have to. -- 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.