我知道地圖可以像這樣訪問:value := someMap["someKey"]要檢查映射中是否存在密鑰,我可以使用以下命令:value, exists := someMap["someKey"]if exists { fmt.Printf("has value: %s", value)}但是,關(guān)于第二代碼,它在這種情況下不起作用:var bag = make(map[string]interface{}, 0)var mux = sync.Mutex{}// Retrieves datafunc Get(key string) (interface{}, bool) { mux.Lock() defer mux.Unlock() return bag[key] // I receive "wrong number of return values (want 2, got 1)compiler"}如何在從函數(shù)返回時從訪問映射中強制返回這兩個值?我使用 Go 1.15bagGet
1 回答

狐的傳說
TA貢獻1804條經(jīng)驗 獲得超3個贊
你必須明確地說你想要這兩個值
// Retrieves data
func Get(key string) (interface{}, bool) {
mux.Lock()
defer mux.Unlock()
v ,ok := bag[key]
return v, ok
}
- 1 回答
- 0 關(guān)注
- 82 瀏覽
添加回答
舉報
0/150
提交
取消