Using backticks vs double quotes from a performance point of view


(Reg) #1

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.


(Jakob Borg) #2

There is no performance difference whatsoever.


(Reg) #3

Thanks.


(Reg) #4

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.


(Karl Benedict) #5

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.


(Jakob Borg) #6

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? :wink: