Skip to content

proposal: Go 2: Use an array as a ExpressionList for switch case statement #58868

Closed
@northbear

Description

@northbear

it would be good to have an able to "unpack" array for switch/case statement in the same way as it can be for variadic function parameters. Example:

var TrueValues []string = []string{"yes", "true", "1"}
var FalseValues []string = []string{"no", "false", "0"}

func Convert(v string) (bool, error) {
    switch v {
    case TrueValues...: 
        return true, nil
    case FalseValues...: 
        return false, nil
    default:
        return true, fmt.Errorf("incorrect value: %s", v)
    }
}

Or maybe make another builtin function (kinda anyof or in in the same way as range) that could be used also with if statement.

func ValidateTrue(s string) bool {
    if s in TrueValues {
        return true
    }
    return false
}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions