go - Complex conditional statement in golang -
i started learning golang , revel. im trying understand below if statement does. seems doing type check, dont see conditional achieves. appreciate if can tell me whats happening here. thanks
if str, ok := obj.(string); ok { return len(str) > 0 }
it tries convert obj (which of abstract interface probably) string, checks if worked, , enters if turned out okay.
written more sparsely can viewed as:
// type assertion/conversion of obj string. // if obj isn't string, ok false str, ok := obj.(string) // run if we're talking string if ok { return len(str) > 0 }
what go safe casting interface real type. if without ok
part, program panic if obj isn't string. i.e code crash program if obj isn't string:
str := obj.(string) return len(str) > 0
you can read more type assertions in docs:
Comments
Post a Comment