I just realized that it's possible to do a map lookup and a type/interface-assertion in one statement.
m := map[string]interface{}{
"key": "the value",
}
if value, ok := m["key"].(string); ok {
fmt.Printf("value exists and is a string: %s\n", value)
} else {
fmt.Println("value does not exist or is not a string")
}
Is this considered bad? I've not seen any official documentation commenting this.
edit: I know this code can not distinguish between "key doesn't exist" and "value is of incorrect type".
edit2: ahem, the print in the else-clause was incorrect :(