... and it's driving me nuts trying to understand what I'm doing wrong!
Playground: https://go.dev/play/p/ZQP8Y-gwihQ
The example looks contrived but it's drawn from code that I have where the error arose. In my code I'm hashing the bytes buffer and want the process to be predictable.
package main
import (
"bytes"
"encoding/gob"
"fmt"
"log"
)
type Foo struct {
Bar string
Baz string
}
func (f *Foo) X() string {
var b bytes.Buffer
s := struct {
Bar string
Baz string
}{
f.Bar,
f.Baz,
}
log.Printf("%v", s)
gob.NewEncoder(&b).Encode(s)
return fmt.Sprintf("%x", b)
}
func (f *Foo) Y(x string) string {
var b bytes.Buffer
s := struct {
Bar string
Baz string
S string
}{
f.Bar,
f.Baz,
x,
}
log.Printf("%v", s)
gob.NewEncoder(&b).Encode(s)
return fmt.Sprintf("%x", b)
}
func main() {
a := &Foo{
Bar: "bar",
Baz: "baz",
}
log.Println(a.X())
log.Println(a.Y("something"))
}
Running yields:
{bar baz}
{1cff81030102ff820001020103426172010c00010342617a010c0000000dff820103626172010362617a00 0 0}
{bar baz something}
{22ff83030102ff840001030103426172010c00010342617a010c00010153010c00000018ff840103626172010362617a0109736f6d657468696e6700 0 0}
Commenting out log.Println(a.X())
yields:
{bar baz something}
{22ff81030102ff820001030103426172010c00010342617a010c00010153010c00000018ff820103626172010362617a0109736f6d657468696e6700 0 0}
I expect the two encodings to the same but they differ (predictably) in locations that I assume correspond to field boundaries:
22
ff83 # 81
030102
ff84 # 82
0001030103426172010c00010342617a010c00010153010c00000018
ff84 # 82
0103626172010362617a0109736f6d657468696e6700
Even though the details differ the behavior is consistent with my code.
I'm creating a new bytes.Buffer
and gob.NewEncoder
in each method and so it's unclear why invoking X
changes the result of Y
.