Generic Methods in Go 1.27

(dominik.info)

41 points | by EspressoGPT 2 hours ago

5 comments

  • DrCiphers 3 minutes ago
    So Go is becoming Java?
  • throwaw12 1 hour ago
    people working actively in "modern" Go codebases with generics, is it becoming like a Java/Spring kind of codebase?

    I understand why you might need generics, but I hate how Java ecosystem exploits it so much that, reading code becomes so difficult, because it was inherited from 10 levels of parent classes and in some cases Java Beans get created based on generic types and their parent classes are abstract classes.

    • throw-the-towel 53 minutes ago
      I remember my coworkers having managed to write Java in pre-generics Go. The code had functions wrapped in functions wrapped in yet more functions, and interfaces only ever implemented by one type to replace the generics abuse. Life finds a way.
      • zimpenfish 19 minutes ago
        Yeah, Go people were always been perfectly capable of creating abominational messes before generics arrived. Once you've looked at an interface referencing an interface referencing an interface referencing an interface which is only ever instantiated by one (1) type, you realise that all hope has been lost and your only recourse is blatant sarcasm, pointed replies on Slack[0] and PRs, and finding the perpetrators after dark with a big sack of flour.

        [0] Other corporate messaging systems are available.

      • bbkane 40 minutes ago
        Entropy finds a way :)
    • riobard 1 hour ago
      > inherited from 10 levels of parent classes

      This is not (yet) a thing in Go. Go does not have classes and inheritance. Interface is more like duck-typing.

      • Zanfa 35 minutes ago
        But you can do equally horrible inheritance-y things with embedded structs.
        • metadat 23 minutes ago
          Yes, though it's non-idiomatic and in my experience, rare to encounter in the wild for anything you'd actually want to use.
    • jerf 28 minutes ago
      "people working actively in "modern" Go codebases with generics, is it becoming like a Java/Spring kind of codebase?"

      I have yet to encounter a 3rd party library that uses generics that isn't a data structure library of some sort. The only person I have seen doing crazy things with generics is... me. In my own code, for the most part [1]. In general I don't even see generics that are so much as parameterized by a meaningful interface like "type BlahBlah[R io.Reader]"; generics are so often parameterized by "any" that a casual reader might be forgiven for thinking that that is all a Go generic can do. As is often the case, the most complicated generic type signatures are the one in the standard library and even they aren't really all that bad, it's really just about the "~" operator allowing support for "all types that are either 'string' or something of the form 'type X string'".

      I see the occasional person complain about how generics have ruined Go by making them too complicated. I push back on them with basically the previous paragraph. None of them have ever replied to press their point any farther. I interpret this to mean that their experience with the feature is in fact the same. At this point if you're buried in super complicated generics it is almost certainly someone on your own team and you should tell them to stop.

      Similarly for iterators, by the way. The way Go did iterators is a bit odd to implement them yourself. Won't deny that. But it does compile down very efficiently, and only matters to the people writing the iterator not the consumer. Anyone who claims it has wrecked the language is invited to explain to me why it is that they are writing so many dozens of iterators all the time first, because as near as I can tell they aren't getting that from 3rd party libraries or the standard library.

      [1]: The exception being https://github.com/thejerf/mtmap if you want to see something that I did find a use for, and you may have a use for, but is not something I would suggest using all the time. Very specific use case I had.

    • eberkund 39 minutes ago
      I think Go is more commonly used in modern microservice architectures rather than in older monolithic architectures where the kind of Java code you're describing happens. Might have some impact on the level of complexity that can get out of hand.

      In the Go codebases I have worked on, generics were not overused IMHO. It did allow us to get rid of some type specific helper functions and made collection based operations like sorting a lot nicer to work with.

  • tancop 2 hours ago
    > Go’s interface system works at runtime. The specific type of a value passed into an interface parameter is resolved while the program runs. This dynamic dispatch clashes with generics being resolved at compile time.

    What if the compiler generated both vtable and monomorphized variants for each method? Most calls would use the static version but places that need it like interface generics call the virtual method.

  • EdSchouten 1 hour ago
    Go is often thought of as a successor of C. C doesn't have methods, only global functions. From my perspective, Go added methods primarily so that you can use them in combination with interfaces. Given that interfaces don't support generic methods, I'm personally not convinced that this feature was worth adding.
    • munificent 1 hour ago
      > From my perspective, Go added methods primarily so that you can use them in combination with interfaces.

      They also give you a limited form of overloading. Without methods or overloading, you end up in the situation that C and Scheme are in where every operation on a data structure has to redundantly have the data structure in its name like:

          list_clear(my_list);
          queue_clear(my_queue);
          map_clear(my_map);
      • kune 10 minutes ago
        In Go you would methods for that: my_list.Clear(), my_queue.Clear() and my_map.Clear(). Now you can define a Clearer interface, which has only the Clear method. That allows you to write a function clearAndLog(item Clearer) and it will work with the list, queue and map.
    • etse 52 minutes ago
      What was the reason for interfaces having to work at runtime?