Go 从函数返回时访问地图时强制返回2个值

Go 从函数返回时访问地图时强制返回2个值,go,Go,我知道可以像这样访问地图: value := someMap["someKey"] 要检查地图中是否存在密钥,我可以使用以下方法: value, exists := someMap["someKey"] if exists { fmt.Printf("has value: %s", value) } 但是,关于第二个代码,它在这种情况下不起作用: var bag = make(map[string]interface{},

我知道可以像这样访问地图:

value := someMap["someKey"]
要检查地图中是否存在密钥,我可以使用以下方法:

value, exists := someMap["someKey"]
if exists {
    fmt.Printf("has value: %s", value)
}
但是,关于第二个代码,它在这种情况下不起作用:

var bag = make(map[string]interface{}, 0)

var mux = sync.Mutex{}

// Retrieves data
func 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"
}

在从
Get
函数返回时,如何从访问
bag
map强制返回这两个值?我使用Go 1.15,你必须明确地说你想要这两个值

// Retrieves data
func Get(key string) (interface{}, bool) {
    mux.Lock()
    defer mux.Unlock()

    v ,ok := bag[key]
    return v, ok 
}

你必须明确地说你想要这两个值

// Retrieves data
func Get(key string) (interface{}, bool) {
    mux.Lock()
    defer mux.Unlock()

    v ,ok := bag[key]
    return v, ok 
}

我知道这很有效。虽然我想知道是否有一种方法可以直接强制返回值和键状态,而不将它们分配给variables@user8555937当前位置没有。我知道它有效。虽然我想知道是否有一种方法可以直接强制返回值和键状态,而不将它们分配给variables@user8555937:没有。