Clubbing values of type switch

Issue

Following code is working fine

var requestMap map[string]interface{}
for _, value := range requestMap {
    switch v := value.(type) {
    case []interface{}:
        if len(v) == 0 {
            // if is empty then no need to throw NA
            return http.StatusOK, nil
        }
    case string:
        if len(v) == 0 {
            // if is empty then no need to throw NA
            return http.StatusOK, nil
        }
    }
}

But following code is giving invalid argument for len function, I have read this question

var requestMap map[string]interface{}
for _, value := range requestMap {
    switch v := value.(type) {
    case []interface{}, string:
        if len(v) == 0 {
            // if is empty then no need to throw NA
            return http.StatusOK, nil
        }
    }
}

Isn’t this case statement enough to identify []interface{} or string as value type ?
Why is it still considering interface{} as parameter of len()

Solution

If you list multiple types in a case of a type switch, the static type of the switch variable will be of the type of the original variable. Spec: Switch statements:

In clauses with a case listing exactly one type, the variable has that type; otherwise, the variable has the type of the expression in the TypeSwitchGuard.

So if the switch expression is v := value.(type), the of v in the matching case (listing multiple types) will be the type of value, in your case it will be interface{}. And the builtin len() function is not allowed to use on values of type interface{}.

Answered By – icza

Answer Checked By – Gilberto Lyons (GoLangFix Admin)

Leave a Reply

Your email address will not be published.