如何進行以下工作並使輸出"Result is: [Value from GetFromMemory]."
?如何獲得接口{}參數以指向其他內容?
不幸的是我不能改變方法簽名GetItem
和Get
。
http://play.golang.org/p/R5me3Q3y4W
package main
import "fmt"
type Key string
type Item struct {
Key Key
Value string
}
func GetItem(key Key) interface{} {
return &Item{key, "Value from GetFromMemory"}
}
// How can I make item point to the one created in GetItem?
func Get(key Key, item interface{}) {
item = GetItem(key)
}
func main() {
var item Item
Get("Key1", &item)
// This should print "Result is: [Value from GetFromMemory]."
fmt.Printf("Result is: [%s].", item.Value)
}
也許GetItem應該返回一個* Item?也許Get應該返回一個接口{}而不是更新一個傳入的變量? –
@匿名OP明確表示他不能更改這兩個函數的簽名。這可能是因爲它們是[GAE數據存儲區API](https://developers.google.com/appengine/docs/go/datastore/reference#Get)的一部分。 – nemo
這完全正確@nemo。我試圖用[Gorilla Context API](http://www.gorillatoolkit)與[GAE Datastore API](https://developers.google.com/appengine/docs/go/datastore/reference#Get)結婚。 org/pkg/context#Get),以便在轉到數據存儲之前檢查結構的內存。 – Dan