我有以下非常簡單的代碼來保留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)
}
}
}
但是它似乎每次都落後一次。下面是一些日誌結果:
C:\Go\GAE\go_appengine\google\src>\go\gae\go_appengine\dev_appserver.py unique/
INFO 2013-10-15 20:55:08,296 sdk_update_checker.py:245] Checking for updates to the SDK.
INFO 2013-10-15 20:55:09,726 api_server.py:138] Starting API server at: http://localhost:50218
INFO 2013-10-15 20:55:09,746 dispatcher.py:168] Starting module "default" running at: http://localhost:8080
INFO 2013-10-15 20:55:09,763 admin_server.py:117] Starting admin server at: http://localhost:8000
INFO 2013-10-15 20:56:22,131 module.py:599] default: "GET/HTTP/1.1" 304 -
INFO 2013-10-15 20:56:22,344 module.py:599] default: "GET /favicon.ico HTTP/1.1" 304 -
INFO 2013-10-15 20:56:22,448 module.py:599] default: "GET /favicon.ico HTTP/1.1" 304 -
2013/10/15 20:56:26 DEBUG: Persisted 38913371956013078496870267859=3378577588146889866220112993
2013/10/15 20:56:26 DEBUG: Persist 38913371956013078496870267859=21186844412818184262771263024
...
2013/10/15 20:56:30 DEBUG: Persist 38913371956013078496870267859=1324177775801136516423203939
INFO 2013-10-15 20:56:30,756 module.py:599] default: "GET /n HTTP/1.1" 200 19
INFO 2013-10-15 20:56:30,927 module.py:599] default: "GET /favicon.ico HTTP/1.1" 304 -
2013/10/15 20:56:32 DEBUG: Persist 38913371956013078496870267859=20778614526287997725322370609
C:\Go\GAE\go_appengine\google\src>\go\gae\go_appengine\dev_appserver.py unique/
INFO 2013-10-15 20:57:15,657 sdk_update_checker.py:245] Checking for updates to the SDK.
INFO 2013-10-15 20:57:17,033 api_server.py:138] Starting API server at: http://localhost:50241
INFO 2013-10-15 20:57:17,085 dispatcher.py:168] Starting module "default" running at: http://localhost:8080
INFO 2013-10-15 20:57:17,098 admin_server.py:117] Starting admin server at: http://localhost:8000
2013/10/15 20:57:24 DEBUG: Persisted 38913371956013078496870267859=1324177775801136516423203939
2013/10/15 20:57:24 DEBUG: Persist 38913371956013078496870267859=20778614526287997725322370609
...
看到持久性存儲最後一次嘗試,但檢索嘗試如何返回的先前堅持的價值。
我在做什麼錯?
注意:我已更改代碼以使用context.Debugf
機制來打印我的調試字符串以獲取等式中的奇怪日誌記錄。
舊的Logf
代碼如下。我確定這是奇怪日誌條目的原因。這不是我的問題的目標。我會自己解決這個問題。
func Logf(format string, a ...interface{}) {
if context != nil {
// Context is valid.
if len(logQueue) > 0 {
// Roll out the stored entries.
for i := 0; i < len(logQueue); i++ {
context.Debugf("%s", logQueue[i])
}
// Empty the queue.
logQueue = make([]string, 0)
}
// Pass a "" to just flush the queue
if format != "" {
// Log it through the context.
context.Debugf(format, a)
}
} else {
// No context! Queue it up.
logQueue = append(logQueue, fmt.Sprintf(format, a...))
}
}
這些都是不一樣的打印語句。沒有「[]」 – stark
我知道這聽起來像一個愚蠢的問題,但你是否在任何地方使用基於'1'的索引?這只是*感覺*就像它從切片中拉出錯誤的值,並且索引被關閉。也許在'NewKey'函數中? – Intermernet
我打算晚些時候發佈我的'Logf'函數來消除在這裏發生切片/數組的想法。我確信格式化和「MISSING」註釋純粹是一種紅鯡魚。請注意,該鍵總是'x string',並且返回的值始終是'string'。 – OldCurmudgeon