Be careful because Go's nil slice will be null in Firestore

problem

I was writing data to a Firestore doc with Go, and for some reason I was wondering if the field I expected an array had null. Also as a data type

type Fuga struct {
	Hoge        []string         `firestore:"hoge"`
}

However, I thought that if there was nothing in the slice, there would be an empty array on the Firestore side and it would be over.

Zero value of slice is nil

Looking at the following text at https://godoc.org/cloud.google.com/go/firestore#DocumentRef.Create, it seems that Firestore treats all types of nil as null.

nils of any type convert to Null.

Since the zero value of the slice is nil, it seems that if you just initialize the structure, the slice will remain nil-> if you save it as it is on the firestore side, it will be null. So, when initializing the structure, I thought it was better to be aware of using make as the slice to create an empty slice that is not nil.

Recommended Posts

Be careful because Go's nil slice will be null in Firestore