I'm running into a really weird issue in my Go code. It seems that identical strings, one declared inside a struct and one outside, have different lengths when used. The following code shows an example:
type evaluateTest struct {
name string
expected int
fen string
}
func TestEvaluate(t *testing.T) {
cases := []evaluateTest{
{"Pawn testing", 330, "8/8/8/8/4P3/3P4/2P5/8 w KQkq - 0 11"},
}
for _, test := range cases {
outside := "8/8/8/8/4P3/3P4/2P5/8 w KQkq - 0 11"
fmt.Printf("String in struct has length %v
", len(test.fen))
fmt.Printf("String outside struct has length %v
", len(outside))
This outputs:
String in struct has length 41
String outside struct has length 38
Looping through the string and printing the character codes gives junk characters in the first three positions (decimal 226, 128, 139) of the string in the struct, and none in the one declared outside.
I'm really at a loss as to what's going on here. Any help is very appreciated.
One string starts with two zero width spaces (\u200b). The other starts with one.
In situations like this, it's helpful to print using %q to see what's going on. See the playground example.