Go language: duplicate string constants compilation -


i'm considering writing template go code generator , wonder:

if have many string constants and/or variables same value stored in executable file multiple times or compiler make optimisation , store 1 instance per duplicate , refer wherever needed?

e.g.

file1.go ======== s1 := "some_string_1" s2 := "some_string_2"  filen.go ======== a1 := "some_string_1" a1 := "some_string_2" 

would affect file size if have multiple files same constants or compiler smart enough make optimization?

imagine in many templates have somthing like:

template 1 ========== {% in list1 %}<td>{{a}}</td>{% endfor %}  tempalte 2 ========== {% b in list2 %}<td>{{b.c}}</td><td>{{b.d}}</td>{% endfor %} 

that translated like:

for i, := range list {     write("<td>")     write(a)     write("</td>") }  i, b := range list2 {     write("<td>")     write(b.c)     write("</td></td>")     write(b.d)     write("</td>") } 

obviously <td> & </td> repeated many times in code. make sense create global array of strings , takes values it? like:

mystrings := [...]string{     "<td>",     "</td>" } i, := range list {     write(mystrings[0])     write(a)     write(mystrings[1]) } 

the language spec says nothing regarding internal storage of string constants or literals. based on discussion assume there no interning in language; https://github.com/golang/go/issues/5160 though bit dated now.

that same search turned project may find helpful; https://gist.github.com/karlseguin/6570372


Comments