Hacker News new | ask | show | jobs
by kodablah 1645 days ago
> I've never even once had to resort to any interface{} trickery to express what I want, and I've written Go programs for Fortune 50 companies, as well as complex personal projects such as AST parsers/code generators.

That's pretty surprising to me. Have you never had to implement marshalers for unknown types and such? I have had to implement things like json.Marshal and json.Unmarshal for different encodings dozens of times in my Go tenure. I have had to use reflection a lot. I have had to deserialize into map[string]interface{} to handle ambiguous situations at runtime a lot. Have you never even had to wrap or build your own Printf equivalents that accept interface{}? No loggers? No custom containers? None of that which operates on unknown types?

I see use of interface{} all over the vast majority of Go projects. I think your experience may be atypical.

3 comments

It's entirely possible that, through some strange quirks of circumstance, I've managed to avoid every problem space that would make me wish for generics.

In spite of that, it's unlikely that I've written implementations where using interface{} would be easier to read and reason about than not using interface{}. And the experience of the author whose blog post we're commenting on tracks with mine: "In my 5+ years working in Go, I can probably count on one hand the number of times that I felt like I really needed generics." I can too, just without using any fingers :-)

I feel the similar way, though I wouldn't be so brave to say I didn't ever use interface{}. I think we all work around slices and maps being the only generic containers and don't know we do. I think everyone will find that while they didn't need generics, they will help them when using utility libraries. Java 1.4 people thought the same.

I expect well-curated libraries to come about that will really simplify some otherwise difficult problems for people (e.g. task/object pooling). I'm even toying with a futures impl at https://github.com/cretz/fut, but I wouldn't use it in place of channels in most cases.

Yes. It would be short-sighted to dismiss them out of hand, and it's possible that generics will provide a level of expressiveness and readability I haven't anticipated yet. I'm fairly bearish on it for now.
> I have had to deserialize into map[string]interface{} to handle ambiguous situations at runtime a lot.

Something I worry about is if I'm getting too jaded. You really think things are different elsewhere, but then you see that we all eat the same shit sandwhich.

That code would never have to be written if someone just used their brain before switching their integer IDs to string GUIDs. Bless your soul but I wish we didn't have to resort to such things. Some things code can't fix.

His experience coincides with my own having solved many business level problems using golang for several years.

At most I have used non-empty interfaces to solve very few number of issues (countable on one hand). I have never needed interface{}.