News for hackers. Stuff that matters.
Re:Half a million lines of Go by auxym on Sunday May 16, 2021 @07:56PM (#27178471)
Sincere question out of curiosity, since I have not been exposed to Go at all.

How do container types, like arrays or hashmaps work without generics? Do you basically have to declare and implement types/methods for each different contained type?

Re:Half a million lines of Go by SiVal on Sunday May 16, 2021 @09:03PM (#27178932)
I hardly ever need generics directly. If I have to write an algorithm for 2 types, I can copy and tweak as easily as I can deal with the added complexity of programming with abstract types.

BUT, I need generics all the time indirectly. I've worked in lots of languages over the decades, and I highly value languages with mature/debugged/optimized standard libraries of algorithms and data types (esp. "containers").

Go has an impoverished subset "built-in", and that's all it offers.

One of the best things about Go is the high quality of its standard libraries--where it has standard libraries--so there is a gaping hole where the container and algo libraries ought to be. Experts in these algorithms could take advantage of Go's concurrency for example to write library algorithms that provide a combination of performance and safety that I couldn't create quickly if at all. They could, that is, if they had generics in the language, but they don't, so I'm on my own (for now).

When people (repeatedly) claim that Go doesn't really need generics because they (the claimants) hardly ever need generics, I have to wonder whether their knowledge of DS and algos is especially high or especially low: that they could whip up a debugged and optimized algo as easily as calling it from a library or whether they aren't even aware of the DS/algo that they really ought to be using.

Re:Half a million lines of Go by catlifeonmars on Monday May 17, 2021 @01:57AM (#27180454)
Personally, it’s very rare that I need to reach for a specialized algorithm. I’m not sure if that’s primarily the problem space I work in (services providing user identity and access management), but I never find the need to go beyond a brute force search or need a data structure other than an array or built in hash map (sometimes a concurrent hash map). Most of the optimization I do involves eliminating network calls and implementing caching.
Re:Half a million lines of Go by void_mint on Sunday May 16, 2021 @08:56PM (#27178872)
Others have answered, but I just wanted to add, your question is usually where beginners/non Go users start to pearl-clutch. I would advise, if you're curious, to give it a shot for a while. As with all new things, try to not just write the language you're used to, but in the syntax of the language you're using, and instead embrace patterns that exist in a given language.

Lots of people find the things you're concerned about missing to not really be the end of the world.

Or you might hate it, and that's also okay.

Re:Half a million lines of Go by zeugmasyllepsis on Sunday May 16, 2021 @08:16PM (#27178628)
Go supports a handful of built-in generic types, like hashmaps. What Go currently lacks is user-defined generics, though that is actively being worked on.
Re:Half a million lines of Go by jimsmart on Sunday May 16, 2021 @08:05PM (#27178535)
Arrays/slices and maps are in fact provided as built-ins that function like generics, e.g. []foo and map[foo]bar work as expected, as do associated helper methods.

But those are the only container types with built-in support like that. (Channels can similarly be of any type)

Re:Half a million lines of Go by takeda on Monday May 17, 2021 @03:09AM (#27180784)
They use hack of "interface{}" which does type checking at runtime (slow). And you then cast it to a proper type.

If I remember correctly (haven't used it in a year) Go cheats a bit, and it has few functions in standard library that do have generics (like make), it just doesn't provide this functionality to the developer.

Re:Half a million lines of Go by loopz on Monday May 17, 2021 @03:57AM (#27181018)
If you can avoid interface{} you should, ie. by declaring interfaces to types and using them explicitly. You can then mix your own types, and still be protected by the type system.
Re:Half a million lines of Go by takeda on Monday May 17, 2021 @12:10PM (#27185083)
Yes, but that requires generics and without them code generators (and if you don't use them, a lot of copy & paste).
Re:Half a million lines of Go by tptacek on Sunday May 16, 2021 @08:30PM (#27178712)
Roughly the same way containers work in Python.
Re:Half a million lines of Go by dragonwriter on Sunday May 16, 2021 @08:52PM (#27178843)
> Roughly the same way containers work in Python.

That’s not really true, since built-in containers are special sui generis generics in Go, whereas in an untyped python they get by being untyped, and in python’s statically-checked type system (as implemented in pyright, mypy, etc.) they use general-purpose generics.

Re:Half a million lines of Go by tptacek on Sunday May 16, 2021 @08:54PM (#27178858)
That complaint is as old as the language --- the first piece about Go I ever read, after my friend Yan convinced me to pick up Go, called it out for having "generics for me and no generics for thee". I've never understood why I'm meant to care. I get, if you want to write your own generic containers, being irritated that there aren't universally available generics (yet). I don't get being upset that the language has some special-cased generics. It's not personal. They're not rubbing salt in your wounds. They just need a map type that works.

At any rate: the person asking the question is wondering how you manage to have performant containers with reasonable interfaces in a language without generics. That's a good question! If you've worked a lot in C, you might be imagining a horrible mess of void-stars or something. But no, the ergonomics are basically those of Python, with extra typing.

Re:Half a million lines of Go by joshuamorton on Sunday May 16, 2021 @09:15PM (#27179033)
Given that they stated "Do you basically have to declare and implement types/methods for each different contained type?" in the comment, I don't think they were wondering if you end up using void*/interface{}.

I really don't understand how the python comparison makes sense. Since those particular containers are special cased, the ergonomics are also the same as java (and I'd argue more like java than python). Other than ownership, I don't think go's maps are any more python like than C++s or Rusts either.

> reasonable interfaces in a language without generics

and with static typing. Python doesn't fall into that category. It either doesn't have static types, or does have generics. The answer (as others have mentioned) is that go does support generics for a few particular container types.

Re:Half a million lines of Go by dragonwriter on Sunday May 16, 2021 @10:39PM (#27179566)
> That complaint is as old as the language

I’m not making a complaint, I’m pointing out a fact without a value judgement.

> At any rate: the person asking the question is wondering how you manage to have performant containers with reasonable interfaces in a language without generics.

Uh, I don’t see either “performant” or “with reasonable interfaces” there, the question is: “How do container types, like arrays or hashmaps work without generics? Do you basically have to declare and implement types/methods for each different contained type?”

The answer is, for built-in containers, “no, because they use special-purpose generics”. For custom containers, its basically “yes”, but you probably only want a single contained type with an appropriate interface anyway. In neither case is “basically the same as Python” particularly accurate, though I suppose the former case is similar to typed python using generics in the same way as any language with generics would be, while the latter is loosely similar to bare Python or typed Python using protocols instead of generics. But only loosely.

Re:Half a million lines of Go by masklinn on Monday May 17, 2021 @04:42AM (#27181290)
> The answer is, for built-in containers, “no, because they use special-purpose generics”.

Unless you want to implement utility helpers working on them then it’s yes again, because Go doesn’t have generic functions either, so while it does have a few builtin generic containers, you can’t operate genetically over them.

Re:Half a million lines of Go by tptacek on Monday May 17, 2021 @12:18AM (#27180008)
Yeah, I'm choosing not to die on this hill. I'm just saying that Go has first-class table and resizable array ADTs.
Re:Half a million lines of Go by masklinn on Monday May 17, 2021 @04:45AM (#27181306)
They’re not first class though: you can not take in, return, or work with the ADTs, only with concrete instances of them.
Re:Half a million lines of Go by tptacek on Monday May 17, 2021 @03:30PM (#27187345)
Hey, there, I'm over on this other hill now, not dying on that one. :)