当路径中的数据不存在时,如何使用Go Firebase-Admin SDK检测空结果

I'm using the following code to get an object from a Firebase realtime database.

type Item struct {
    title string `json:"title"`
}
var item Item
if err := db.NewRef("/items/itemid").Get(ctx, &item); err != nil {
    log.Infof(ctx, "An error occured %v", err.Error())
}
log.Infof(ctx, "Item %v", item)

If no data exists at the given path in the realtime database the SDK will not return an error, instead I will end up with an empty struct in the variable item.

What would be the cleanest/most readable way to detect that the data at the path is not there?

I've searched for hours but couldn't find a clear cut answer to this question.

Here's one way to solve this problem:

type NullableItem struct {
    Item struct {
        Title string `json:"title"`
    }
    IsNull bool
}

func (i *NullableItem) UnmarshalJSON(b []byte) error {
    if string(b) == "null" {
        i.IsNull = true
        return nil
    }

    return json.Unmarshal(b, &i.Item)
}

func TestGetNonExisting(t *testing.T) {
    var i NullableItem
    r := client.NewRef("items/non_existing")
    if err := r.Get(context.Background(), &i); err != nil {
        t.Fatal(err)
    }
    if !i.IsNull {
        t.Errorf("Get() = %v; want IsNull = true", i)
    }
}

As a best practice you should also implement MarshalJSON() function.