Stale field data. Bug?

#1

Hi,

Couchbase 4.0 CE
Golang 1.6.2, Mac OS
gocb driver - last go get -u from July 8, 2016.

Context
I have 2 records. The only difference between them is that the 1st one has a field “imageFull” while the 2nd one does not have this field.

I’m doing a regular loop over the n1ql results as per http://developer.couchbase.com/documentation/server/4.0/sdks/go-beta/querying.html:

var row MyStruct
for rows.Next(&row) {

}

Issue
A record with a missing “imageFull” field (at least as per Query Workbench) , still contains this field with a value from a previous record.

Please advise.

#2

Hey @dennis,

This is due to the behaviour of Go’s JSON decoder. It only will modify fields which actually exist within the JSON. Because you are reusing your row through multiple passes, and later versions of the JSON do not contain your imageFull field, that field is not being touched. The solution is relatively simple (just reset the structure after each row):

var row MyStruct
for rows.Next(&row) {
  //...
  row = MyStruct{}
}

Cheers, Brett

#3

@brett19

That worked!

Thanks so much,
D