A more anecdotal answer than @evanmcdonnal's is that, at least in go1.22,
package main
import "fmt"
const (
str1 = "asdf"
str2 = "ghjk"
)
func main() {
fmt.Println(str1, str2)
}
and
package main
import "fmt"
const (
str1 = "asdf"
str2 = "asdf"
)
func main() {
fmt.Println(str1, str2)
}
compile out to the same size. So the compiler doesn't even optimize that within the same file.
$ go version
go version go1.22.2 darwin/arm64
$ go build -o diff diff.go
$ go build -o same same.go
$ ls -l
total 7952
... 2029586 Jan 14 17:32 diff
... 110 Jan 14 17:31 diff.go
... 2029586 Jan 14 17:32 same
... 110 Jan 14 17:31 same.go