Does anyone know whether you should use backticks every time you don’t have a string that needs interpolation?
To elaborate, in a language like PHP you would use single quotes to avoid interpolation whenever practical because interpolation can cost an overhead. However, since go is a compiled language it could be that the compiler takes care of all this so it doesn’t really matter in Go from a performance point of view whether a string uses backticks or quotes.
I’m asking because I see a lot of Go code using double quotes where backticks would be perfectly fine giving me the sense that it won’t make any performance difference in Go.
There is no performance difference whatsoever.
So that would mean the only reasons to use backticks are just personal preference or if you just want to use backticks for an additional visual cue and then if the string changes so you need quotes you are giving yourself extra work.
It makes you wonder if backticks have any real value in go except I guess if you have a string with a lot of quotes in it it would make the string easier to read using backticks to enclose the string.
Also for regexp with backslashes in it, also for struct tags. Quote marks are not the only thing you need to escape in regular double quoted strings.
Yes, the two are different. Double-quote strings parse escapes like
\n, backtick-strings do not. They compile down to the same bytes, but one is more convenient than the other in some circumstances. Like being able to specify numbers in decimal, hex, octal. Does that have “real value” when we could just write them in binary?
This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.