7 ms·
Is Go an object-oriented programming language?
- lastangryman 4y agoIMO, Go is capable of supporting OOP style code, but is not an OOP language. Coming to Go from more dogmatic OOP languages is such a breath of fresh air. If you just need a struct, you can have a struct. If you want a package scoped standalone function, you don't need to invent some made up "class" to attach it to. As the article shows, you can implement OOP style code and use OOP paradigms when they make sense. With Go, they emerge more naturally from the code as you are not trying to force them in from the start. These are probably trivial things to a lot of people but after being brought up on Java, and working in rather OOP-heavy frameworks for years, it's liberating.
- fhd2 4y agoI believe it's called multi paradigm languages - most I've used are like that (C++, Kotlin, JavaScript, Python, ...). The only dogmatic OOP languages I can think of are Java (where it is IMHO nastily done), Ruby and Smalltalk (in both of which I never felt the burden of OOP boilerplate). I think it's a Java problem, not an OOP problem.
- kaba0 4y agoA static method is basically a “standalone” function with a namespace only. You can static import it and do whatever you want. Java is a multi-paradigm language, and is absolutely on a different level of expressivity compared to Go. EDIT: Java is really not what people remember from 20 years ago. You can write as functional code as you wish stopping just before Monads. It’s type system is also a different league than Go’s. So it has OOP, imperative and FP.
- pjmlp 4y agoSince most early Java adopters were Smalltalk shops, I fail to see the difference. I used Smalltalk/V before Java came to be, by the way.
- lastangryman 4y agoYes, good point. I worked in a lot in PHP though and interesting that a lot of the changes it has went through the last 15 years seem to have been "inspired" by Java. A lot of frameworks (Laravel, Symfony, Zend) seem to lean heavily on OOP style, and it's easy (or it was for me anyway) to fall in to the trap of everything must be a class and if not you are doing it wrong, even if the language does not require OOP. Design Patterns, Refactoring, Clean Code, it feels like a lot of my "industry journey" if you like was built on an assumption that everything started with OOP. Go, for me, was the first language I've used in a while that truly detached itself from all that dogma, not just in it's features but it's overall principles and ethos.
- dagw 4y agoJava's "problem" is more cultural and historical than technical. Modern Java has lots of support for functional and multi-paradigm programming. However most non-trivial Java codebases are quite old and don't take advantage of these newer features, plus most Java programmers were trained in the traditional Super-OOP style of Java programming and still program and advocate for that style of programming.
- ThePhysicist 4y agoI don't think you can call a language object-oriented if it doesn't support some form of inheritance and dynamic dispatch. And for Golang that's simply not the case. You can embed data structures into other structures but the main difference to e.g. C++ is that you don't have proper RTTI (run-time type information) that would do a dynamic dispatch of struct methods, so you can't do anything interesting (i.e. you can embed an "Animal" struct in a "Dog" struct but if you define the ".MakeNoise()" method on the Animal struct and have that call a ".myNoise()" method on the struct to e.g. produce "Woof" or "Meow" then even if you override ".myNoise()" in the "Dog" struct the "Animal" struct will still call its own ".myNoise()" method, as there's no dynamic call table like in C++). People will probably say that's by design but I'd then argue again that's not object-orientation as you can do these things with C structs as well and no one would call C an object-oriented language.
- orwin 4y agoI think traits are a big improvement on inheritance, do you count those as as some form of inheritance? In this case I can agree with you. Personally, when I have to use it, I make abstract classes or better, interfaces. I avoid direct inheritance like the plague. But I've worked on personalized Jenkins where I had to inherit classes from weird modules, so maybe I got burned and my experience isn't the average experience (also, I hate Java now).
- ThePhysicist 4y agoIn my understanding traits (I guess you mean as defined in Rust?) are more powerful than interfaces in the sense that you can define them for objects from the outside, but I don't see how you could use them to build dynamic-dispatch like functionality? Not an expert on traits at all though. I think traits and interfaces are more supportive of a composition-like programming style, one might call that object-oriented but I still think it misses several key concepts of classical object-oriented languages.
- bheadmaster 4y agoIn Go, interfaces are used for dynamic dispatch. No need to the the whole ".MakeNoise() calls .myNoise()" dance, just implement ".MakeNoise()" on each type directly: type Animal interface { MakeNoise() } type Dog struct{} func (Dog) MakeNoise() { fmt.Println("Woof!") } type Cat struct{} func (Cat) MakeNoise() { fmt.Println("Meow.") } func MakeNoises(as ...Animal) { for _, a := range as { a.MakeNoise() } } func main() { d := Dog{} c := Cat{} MakeNoises(d, c) }
- friendzis 4y ago> If you want a package scoped standalone function, you don't need to invent some made up "class" to attach it to. It's a matter of language construct organization. There are bare behaviors - functions, there is bare state - PODs, and then there are collections of state and behavior - classes. You can have these three distinct constructs, or you can have all them be subcases of class (or some other mix). It's just that we are used to having distinct primitive and collection data types, but nothing (sans terseness) stops language from having all data be some form of collection, even if it is a single-member collection.
- kaba0 4y agoIt can as much as every Turing-complete language can support any programming model given enough abstraction.
- fanf2 4y agoWorth reading Felleisen’s paper “on the expressive power of programming languages” (or watch this talk https://youtu.be/43XaZEn2aLc https://youtu.be/43XaZEn2aLc) which argues that if a language A has a feature that would require global refactoring to implement in language B, then A is more expressive than B.
- kaba0 4y agoSure, I was just being sarcastic on parent’s claim not meaning too much ad absurdum. Nonetheless, thanks for the paper/video, will look into them!
- deleted 4y ago[deleted]
- lelanthran 4y ago> Coming to Go from more dogmatic OOP languages is such a breath of fresh air. There are only a few (two?) dogmatic OOP languages in common use. Every other language that supports OOP has better OO support than Go, as well as: > If you just need a struct, you can have a struct. If you want a package scoped standalone function, you don't need to invent some made up "class" to attach it to. While I like Go, a lot of Go code could be simplified by having keyword-supported OO (a 'class' keyword, maybe). It's better to have inheritance and not need it, than need inheritance and not have it because it is applicable in a lot more places than "composition over inheritance" fanboys would have you believe.
- za3faran 4y agoWith records now available in Java and C#, not only can you do the same there, but you get even more assurances such as overloaded `.equals()` and `.hashCode()`.
- Cthulhu_ 4y agoI like to think of it as a language that gives you some of the developer reasoning ability of an OO language - data & behaviour encapsulation - without the overly complicated push for lasagna code that e.g. Java tries to teach you. No dog extends animal, cat extends animal, duck extends animal hierarchy, but a "bark" method that only dog and cat implement. No "everything is an object", you get to decide what goes on the stack vs the heap based on your own insight. But also no pointer arithmetic footgun.
- kaba0 4y agoAn object has nothing to do whether it is allocated on the stack or the heap. C++ has had stack-allocated objects for decades. Also, favoring composition over inheritance has been a major mantra of Java since forever.
- fabian2k 4y agoC# is a clearly object oriented language, and you can decide what is on the stack and on the heap. And while inheritance is a very prominent feature of object oriented languages, it is not the only option there. I think the way those languages are taught over-emphasizes inheritance, but there are plenty of arguments today around preferring composition. So I don't think this is an inherent property of the languages, they allow you to use inheritance but you're not forced to used it everywhere.
- tjpnz 4y agoI can also model encapsulation, inheritance and polymorphism in C. That doesn't make it an object-oriented programming language.
- orwin 4y agoI'm pretty sure you can do OOP in modern C. I've seen a raytracer in C that implemented it's own dispatch table, and effectively acted like OOP.
- Rochus 4y agoYou can also do OOP in assembler if you have nerves of steel.
- shric 4y agoAnd Alan Kay (first user of the term "object oriented programming") said: "OOP to me means only messaging, local retention and protection and hiding of state-process, and extreme late-binding of all things. It can be done in Smalltalk and in LISP. There are possibly other systems in which this is possible, but I'm not aware of them."
- Areading314 4y ago[flagged]
- noisy_boy 4y ago> By now, those of you from the classic OO camp have found the reason why you “felt awkward” when you started with Go! I did bunch of Java before doing Go but also did bunch of non-OO plain vanilla scripting before that so have background in both OO and non-OO procedural approaches - I found Go's approach very "natural". It was not awkward but "new" and once I started coding, things just felt in place. I was productive quite quickly. If only they had solutions to reduce the if err != nil boilerplate plus support for some comprehensions to allow more concise code, Go would easily be my favorite language (even with all that, I still like Go).
- pjmlp 4y agoRegarding the usual discussions of using interface based OOP, we have "Component Software : Beyond Object-Oriented Programming", specially the first edition that focus on Component Pascal, Java, while the 2nd edition was not only focused on languages, but also IDL based components like COM/ActiveX/CORBA/RMI/.... Additionally "Traits - Composable Units of Behavior" as evolution from Smalltalk object model, or protocols and categories in Objective-C, functors in OCAML (nee Objective CAML), clusters in CLU. As yet other set of examples where interfaces, modules and polymorphism drive the OOP story without classes as the main "actor".
- underdeserver 4y agoThe newest common languages, Go and Rust, have both left objects with type hierarchies behind. Seems modern language designers think inheritance is bad. I tend to agree: the relationship is so confusing to get right (is a Square a Rectangle?), that composition instead of inheritance and Go-style interfaces instance of explicit interfaces are what you actually want in almost all cases.
- kaba0 4y agoThere are problems which are not solvable nicely without inheritance. They are rare, but GUIs are the stereotypical example, where the `SomeWidget extends Node` is just the most useful abstraction to have. Also, go is modern only in age, the only remotely novel thing about it is goroutines, so I wouldn’t detract too much from its design choices.
- gpderetta 4y agoAlgol and Simula had coroutines (just to give two examples). Go just made them mainstream again.
- kaba0 4y agoCoroutines are not just the same as “virtual threads” (in a different lingo) These concurrency models can be placed on a matrix based on stackfull/stackless and based on preemptive/cooperative. I think there are/were examples for all of them — the more interesting property of goroutines (and Java’s virtual threads) is turning blocking calls into non-blocking, by using the OS’s async IO behind the scenes for everything.
- za3faran 4y agohttp://cowlark.com/2009-11-15-go/ http://cowlark.com/2009-11-15-go/ :)
- pjmlp 4y agoInterfaces also form type hierarchies.
- bheadmaster 4y agoI think the modern definition of "object-oriented language" is skewed too much in the direction of "what C++ is doing". The OOP paradigm, as far as I remember, relies on the four pillars: - encapsulation - abstraction - inheritance - polymorphism All of which exist in Go (encapsulation: structs; abstraction: interfaces; inheritance: struct embedding; polymorphism: interfaces). Nobody said that inheritance must be polymorphic, like it is in C++ with virtual methods. Nobody said we must have the "class Class { members... }" syntax like we have with C++. Those are all just implementation details. Go is a perfectly good OOP programming language, it just differs from C++, so C++-ians who can't see anything beyond the syntax claim it's not OOP.
- JonChesterfield 4y agoWhat would inheritance without polymorphism be?
- bheadmaster 4y agoI don't know, and I never mentioned inheritance without polymorphism, so I don't see how its relevant. Go supports both inheritance and polymorphism. It just doesn't have polymorphic inheritance, which doesn't really bring you any new abilities that polymorphism and inheritance themselves didn't bring. Please see this comment for an example of Go's polymorphism and inheritance working together to do what you would need virtual methods for in C++: https://news.ycombinator.com/item?id=35784598 https://news.ycombinator.com/item?id=35784598
- JonChesterfield 4y ago> Nobody said that inheritance must be polymorphic Polymorphim normally means dispatch based on type of the first argument to a function, sometimes called a method. Occasionally dispatch based on type of all arguments to a function. Inheritance is a means of deriving types from other types. That's usually done as a means of controlling dispatch via the above polymorphism. I don't know of a different motive for inheritance. You claimed inheritance doesn't need to be polymorphic, as quoted above, so I hoped you had a different perspective on it.
- mrfinn 4y agoThe answer is definitively no, it's a procedural oriented language. But that doesn't goes against the fact that also supports/emulates quite a few interesting object-oriented features, while maintaining syntax as simple as possible, and that is what made it so interesting for me. How can it be an object-oriented language when it doesn't even support the concept of objects/classes? It was born actually as an act of rebellion against the complexity reached by C++ AFAIK. One observation from my noob POV: Packages and structures functionalities seem to overlap at some points and it's not so easy to understand when to use each one at first.