Hacker News new | ask | show | jobs
by donatj 3143 days ago
> intuitively you'd think that you could assign any type slice to []interface{} but you find out soon enough that doesn't work

I've been writing Go for upwards of six years, professionally for three, and I have never tried that. I can't think of the last time I used interface{}...

I generally consider anything using interface{} crap code someone who doesn't know the language wrote, probably coming from a loosely typed language.

1 comments

> I generally consider anything using interface{} crap code someone who doesn't know the language wrote, probably coming from a loosely typed language.

Disagree. There's ample usage of interface{} that you need to type-switch on in parts of the stdlib that deal with the AST and reflection or various other areas. You might also UnmarshalJSON a structure where some field can contain any one of a number of possible sub-structures. Once we're talking about 1-2 dozens of possible sub-structures, you'll have that field be an interface{} and type-switch on it --- rather than having 1-2 dozen pointer fields that will all-but-1 be nil and having to if-else through them all.