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:

http://golang.org/ref/spec#type_assertions


Comments

Popular posts from this blog

database - VFP Grid + SQL server 2008 - grid not showing correctly -

jquery - Set jPicker field to empty value -

.htaccess - htaccess convert request to clean url and add slash at the end of the url -