On Thursday, August 3, 2017 at 12:55:44 PM UTC-4, Kaviraj Kanagaraj wrote: > > Can someone help me understand whats the main difference two different > UserType in below snippet.? > > More importantly when to use which. > > ``` > type UserType string > > const ( > Undefined UserType = "undefined" > Admin UserType = "admin" > ) > > type UserType int > > const ( > Undefined UserType = iota > Admin > ) > > func (u UserType) String() string { > switch u { > case Undefined: > return "undefined" > case Admin: > return "admin" > } > return "UserType" + strconv.Itoa(int(u)) > } > ``` > > Some obvious differences including > 1. More memory usage in case string based type >
why? > 2. Usefull zero-value in case of int based type. > > I just wanted to know is there any rule of thumb to decide when to use > which. > Besides we can declare methods on custom types, custom types will get a better readability and can avoid mis-assignments. > > Thanks in advance. > -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.