In Go, strings are stored internally as the C-struct:
struct String // This is C code (not Go)
{
byte* str;
int32 len;
};
Let's say I have the following variables:
a0 := "ap" // This is Go code
a1 := "ple"
b0 := "app"
b1 := "le"
a := a0 + a1
b := b0 + b1
c := "apple"
d := c
Then the following code:
fmt.Println("a == b = %t, &a == &b = %t", a == b, &a == &b)
fmt.Println("c == d = %t, &c == &d = %t", c == d, &c == &d)
outputs:
a == b = true, &a == &b = false
c == d = true, &c == &d = false
because &a == &b
compares the addresses of the C-structs, while a == b
compares the values of the strings.
Is there any way to test whether the strings themselves are stored in the same place (i.e. the str
field in the C-struct has the same value), such that comparing a
and b
would most likely yield false
, while comparing c
and d
would almost certainly yield true
?