Uncomparable type error when []string field used (Go lang) -
i have particular go lang struct object i'm interacting , expect equal itself. i'm passing function function returns accepting interface{} inputs/outputs
type animal struct { name string food interface{} } type yummyfood { calories int } func echo_back(input interface{}) interface{} { return input } func main() { var tiger_food = food{calories: 1000} var tiger = animal{name: "larry", food: tiger_food} output_tiger := echo_back(tiger) fmt.printf("%t, %+v\n", tiger, tiger) fmt.printf("%t, %+v\n", output_tiger, output_tiger) fmt.println(tiger == output_tiger) fmt.println(tiger == output_tiger.(animal)) }
running this, see tiger , output_tiger appear same , evaluate equal. great. that's expect. now, try using definition yummyfood
type yummyfood { calories int ingredients []string }
all of sudden, output echo_back not evaluate same input, type assertion. 'panic: runtime error: comparing uncomparable type yummyfood'
the question why addition of []string type cause input , output uncomparable? how can modify output_tiger same thing put in (i.e expect same 'tiger'). how reflect output_tiger make equal tiger?
slices ([]string
) has no equality defined. arrays , structs contains members has equality defined have equality defined.
note equality still undefined slices, calculation in general infeasible. note ordered comparison operators (< <= > >=) still undefined structs , arrays.
Comments
Post a Comment