GAE Go-如何将私有变量放入数据存储区?

I am writing a Google App Engine Golang application. I want to have a struct with private variables that can only be set through proper functions, for example:

type Foo struct {
    bar string
}

func (f *Foo) SetBar(b string) {
    f.bar = "BAR: "+b
}

I want to be able to save this data in the datastore. However, it looks like datastore does not save private variables.

How can I store private variables in datastore?

You can if your types implement the PropertyLoadSaver interface:

func (f *Foo) Save (c chan<- datastore.Property) error {
    defer close(c)

    c <- datastore.Property {
        Name: "bar",
        Value: f.bar,
    }

    return nil
}

func (f *Foo) Load(c <-chan datastore.Property) error {
    for property := range c {
        switch property.Name {
        case "bar":
            f.bar = property.Value.(string)
        }
    }
    return nil
}

The downside is that you will need to load/save all your properties by hand because you can't use the datastore package methods.