我有以下非常简单的代码来在datastore. 我已经从各种datastore例子中把它汇总起来,但我仍然对它不满意。
它的目的只是将一个字符串存储在一个键下persist并在 下检索它fromPresistence。
// Used to store the string value.
type Entity struct {
Value string
}
// Grow my key.
func key(x string) *datastore.Key {
return datastore.NewKey(context, "Persist", x, 0, nil)
}
// Get it from persistence storage.
func fromPersistence(x string) string {
var persisted string = x
// Make my key.
k := key(x)
// New entity for filling in.
e := new(Entity)
// Look it up!
if err := datastore.Get(context, k, e); err == nil {
// It was there!
persisted = e.Value
context.Debugf("Persisted %s=%s", x, persisted)
}
return persisted
}
// Persist the latest number.
func persist(x string) func(*big.Int) {
return func(n *big.Int) {
// Make my key.
k := key(x)
// New entity for filling in.
e := new(Entity)
// Value is the decimal form of the number.
e.Value = n.String()
context.Debugf("Persist %s=%s", start, e.Value)
if _, err := datastore.Put(context, k, e); err != nil {
context.Debugf("Persist failed! %s", err)
}
}
}
呼啦一阵风
相关分类