What Go does better than Java and reverse from your experience?

polaris · · 740 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>I&#39;ve started to learn Java mainly because Android development is based on it. I&#39;m not really familliar with it, but I&#39;ve a c++ background that might give me some ideas. What I&#39;m really looking to know is where I should use Java instead of Go. There&#39;s some fields below that I&#39;m interested in and wanna hear your opinions as well:</p> <p>a) Mobile Development </p> <p>b) Web Development</p> <p>c) Games Development</p> <p>d) Scientific Development</p> <p>e) GUI Development</p> <p>f) New field..</p> <p>If you&#39;re familliar with both languages and wanna give me your opinion would be awesome! </p> <p>Note that I&#39;m not looking to switch or give up one language in favor of another, but wanna learn to use them where they fits the best.</p> <hr/>**评论:**<br/><br/>shovelpost: <pre><p>The only things I can think of that Java does better:</p> <ul> <li>Android development (Duh!)</li> <li>Usage in enterprise</li> <li>GUI in their standard library</li> </ul></pre>tetroxid: <pre><ul> <li>Generics</li> </ul></pre>shovelpost: <pre><pre><code>public static &lt;I, O&gt; ListenableFuture&lt;O&gt; chain (ListenableFuture&lt;I&gt; input, Function&lt;? super I, ? extends ListenableFuture&lt;? extends O&gt;&gt; function) </code></pre> <p><a href="https://youtu.be/5kj5ApnhPAE?t=2m8s">dear god make it stop</a></p></pre>vorg: <pre><p>Go would choose a better syntax. For your example Go would choose something like the more readable</p> <pre><code>func [I, O] chain (inp ListenableFuture[I], fn func[I:, :ListenableFuture[:O]]) ListenableFuture[O] </code></pre> <p>to convey the same information. Though of course Go would convey different information, not needing to convey <em>extends</em> and <em>super</em> with prefixed and postfixed colons, for example.</p> <p>If ListenableFuture was defined in a different package, you would have</p> <pre><code>import u &#34;somedir/future&#34; func [I, O] chain (inp u.Listen[I], fn func[I:, :u.Listen[:O]]) u.Listen[O] </code></pre> <p>where short names <em>u, inp</em>, and <em>fn</em> make reading it easier.</p></pre>VirmundiUndead: <pre><p>I admit that is noisy, but if you take time, you can tell what that is going to do with just that line. You might need to go see what ListenableFuture is, but that line tells you everything you will need to get more information. You won&#39;t need to dive into some internals to see what&#39;s what. The strong, noisy typing, provides that info. </p></pre>sh41: <pre><pre><code>package task // DataᐸTᐳ where T is int. type Dataᐸintᐳ interface { IsCollection() bool GetCollection() CollectionᐸDataᐸintᐳᐳ GetElement() int } // Java&#39;s CollectionᐸEᐳ interface (partial). // CollectionᐸEᐳ where E is Dataᐸintᐳ. type CollectionᐸDataᐸintᐳᐳ interface { IsEmpty() bool Iterator() IteratorᐸDataᐸintᐳᐳ Size() int // ... more methods are available in the real Java CollectionᐸEᐳ interface, // see http://docs.oracle.com/javase/7/docs/api/java/util/Collection.html. } </code></pre> <p><a href="https://github.com/shurcooL/play/blob/9ee6a378d9541f5ef9d36b295915bd0818dfdaa9/103/given.go" rel="nofollow">https://github.com/shurcooL/play/blob/9ee6a378d9541f5ef9d36b295915bd0818dfdaa9/103/given.go</a></p></pre>xrayfur: <pre><p>These points are more what Java <strong>is</strong> rather than what it <strong>does</strong>. But yeah, good point!</p></pre>shovelpost: <pre><p>Yeah very true. I was debating which verb to use actually but then I noticed that each bullet technically needs a different verb so I was like screw this. :P</p></pre>Alaric101: <pre><p>It&#39;s a minor thing, but it would be nice if there was a way of denoting that a class is an implementation of an interface like Java does.</p></pre>shazow: <pre><p>This will assert that someImpl complies with someInterface when you compile:</p> <p><code>var _ someInterface = (*someImpl)(nil)</code></p></pre>shovelpost: <pre><p>That already exists and it&#39;s called <a href="https://docs.google.com/document/d/1_Y9xCEMj5S-7rv2ooHpZNH15JgRT5iM742gJkw5LtmQ/edit">Go Guru</a>.</p></pre>Richy13: <pre><p>This looks great, hadn&#39;t heard of it</p></pre>saturn_vk: <pre><p>Yep. Its unfortunate that the go plugin for vscode lacks quite a few things, including that, compared to other plugins (like vim-go). asking for implementations is quite helpful.</p></pre>comrade-jim: <pre><p>oh no, you mentioned VSCode in a negative light, here comes the shill team to downvote you (even though everything you said was true). </p></pre>BubuX: <pre><p>As a VSCode user that&#39;s lazy to thoroughly test other IDEs. What editors have better support for Go than VSCode?</p> <p>I might try them.</p></pre>Alaric101: <pre><p>Thanks, I&#39;d never heard of this!</p></pre>jeremiahs_bullfrog: <pre><blockquote> <p>denoting that a class is an implementation of an interface</p> </blockquote> <p>You mean struct? Go doesn&#39;t have classes.</p> <p>I typically see this done in documentation, like &#34;A is a Reader&#34;.</p></pre>sorenmat: <pre><p>XML with namespaces</p></pre>shark1337: <pre><p>Yeah, I was thinking the same. I&#39;m pretty sure that java might handle game development better, Minecraft is a good example here..</p></pre>thomascgalvin: <pre><p>From a gaming point of view, having a GUI backed into the standard library is good, and there are a bunch of established third-party libraries that allow you to write OpenGL, so I think Java has a leg up on Go in that department.</p> <p>I would be curious to see how Go&#39;s garbage collection compares in a gaming environment, though. I&#39;ve done some game programming in Java, and I have to be very careful to prevent the garbage collector from running. I wonder if that would be necessary with Go&#39;s lower-overhead GC.</p></pre>PsyWolf: <pre><p>You mean lower latency. Go achieves such low latency (aka low pause times) at the cost of higher overhead (aka lower throughput). In theory, Java can be configured to use a similar low latency garbage collector like go&#39;s, but I don&#39;t know how it compares in practice for game development. </p></pre>edapa: <pre><p>There is also the fact that typical go programs tend to generate less garbage than idiomatic java doing the same thing.</p></pre>PsyWolf: <pre><p>Do you have a source to go with that fact? Legitimately curious.</p></pre>edapa: <pre><p>Nothing satisfying, just internet hearsay and the crawling sensation that I get on my skin when I see how many time the word <code>new</code> shows up in a Java program :P. Sorry for spreading vicious rumors.</p></pre>DeedleFake: <pre><p>I&#39;ve had a sneaking suspicion for a while that this is why Java does so well in microbenchmarks but large Java programs always feel terrible. Java&#39;s particular OO style seems to lend itself towards horrendous numbers of allocations and context switches in large projects. </p></pre>carsncode: <pre><p>And reflection. Java is a perfectly capable language, yet somehow no one can seem to use it without adding at least one or two frameworks that use reflection extensively.</p></pre>thomascgalvin: <pre><p>Right, latency. The Java GC is actually swappable, and you can drop in a version that does the &#34;right thing&#34; for your peculiar situation, but I don&#39;t have the kind of knowledge required to fuck around with it.</p></pre>hygutyughugug: <pre><p>I suggested this for Go awhile back, but it was shot down. Their argument was that it would be more knobs, which created more complexity - they really like to throw that word around.</p> <p>the type of work I was doing at the time didn&#39;t have a need for low latency, and needed high throughput, so I brought it up on go-nuts, but it was rejected. Supposedly the complexity wasn&#39;t worth the hassle. I would love to build with different gc tailored to specific goals, like throughput over latency. A few programs from that project now suffer a noticeable performance penalty, so much so that they loath rebuilding the programs with new releases. Nothing can be done on our end.</p></pre>edapa: <pre><p>The JVM does expose lots of black magic knobs for tuning the GC, but I think you can almost always just fiddle with the target GC pause time and it will mostly do the right thing. The JVM maintainers have tried hard to make GC tuning simpler recently. (Disclaimer, I&#39;m not a java GC wizard, I&#39;ve just read some stuff written by one).</p></pre>shovelpost: <pre><blockquote> <p>I&#39;m pretty sure that java might handle game development better, Minecraft is a good example here..</p> </blockquote> <p>I don&#39;t think either Java or Go particularly shine at game development but neither of them have any characteristics that make them totally unsuitable for writing games either.</p> <p>Minecraft is a very special case and I&#39;d argue not a very good example. It could have been written in any language and still be the success it is because... Minecraft.</p> <p>But I am going to agree that Java has the edge in terms of game development over Go. Not only because successful games already exist written in Java but also because Java has better graphics/engine tooling available as well. </p> <p>Also if we think about Android game development in particular, then obviously Java has no competition there.</p></pre>hygutyughugug: <pre><p>Show me a game written in Go. Go has nothing on java in regards to game programming. Java still rules the web. I can give hundreds if not thousands of java examples, from web, to android, to native.</p> <p>Biggest mistake Go ever made was focusing on server side programming. If Go had a decent graphics/gui tool kit it would have leaped past java years ago.</p></pre>shovelpost: <pre><blockquote> <p>Show me a game written in Go.</p> </blockquote> <p><a href="http://blockbros.net/tsugunai/en/" rel="nofollow">http://blockbros.net/tsugunai/en/</a></p> <blockquote> <p>Go has nothing on java in regards to game programming.</p> </blockquote> <p>I don&#39;t disagree but in my comment I wanted to emphasize that it is the Java <em>ecosystem</em> that has had more years to develop better tooling for games. But as <em>languages</em> they should be pretty much equal when it comes to writing games. And by equal I mean that both languages are not particularly amazing for writing games but they are not totally unsuitable either.</p> <blockquote> <p>If Go had a decent graphics/gui tool kit it would have leaped past java years ago.</p> </blockquote> <p>I agree.</p></pre>hygutyughugug: <pre><blockquote> <p><a href="http://blockbros.net/tsugunai/en/" rel="nofollow">http://blockbros.net/tsugunai/en/</a></p> </blockquote> <p>Seems it is a hybrid game. You get half credit :)</p></pre>shark1337: <pre><p>I agree with you, the only language that fits for writting game is c/c++, I don&#39;t think I&#39;ll be developing games doe..</p></pre>Redundancy_: <pre><p>Minecraft was a popular, good game, not necessarily well-engineered.</p> <p>There&#39;s also an important distinction between client-side and backend - game clients tend to be written on whatever the platform best supports, and backends often follow suit due to reuse of code. C++ rules the roost in professional AAA circles. </p> <p>That said, I know of multiple large studios using some Go in some form (usually not exclusively).</p></pre>alasijia: <pre><p>For web dev, low memory consumption, short startup time duration and no needs to use frameworks.</p></pre>VirmundiUndead: <pre><p>I think the no frameworks argument only goes so far. If you start having complex systems, or just want to decouple instantiation, you&#39;ll want a DI framework. So then you could create one, wait for some else to create one, or just use Spring. It&#39;s fairly light weight now. With spring and JPA, you&#39;ll probably get moving fairly quick. Spring and Spring JDBC is probably going to get you farther and faster than Go defaults. </p></pre>_ak: <pre><blockquote> <p>If you start having complex systems, or just want to decouple instantiation, you&#39;ll want a DI framework.</p> </blockquote> <p>Why? I&#39;ve maintained projects with literally millions SLOCs, and never needed one. Why would I need a DI framework?</p></pre>VirmundiUndead: <pre><p>So you can break your layers apart. Say you have a controller layer and a DB layer. If your controller instantiates your DB classes, you&#39;re coupled. If you have a constructor for test and a default constructor, you now have two flows. If you use DI, you only have one flow and full decoupling. </p></pre>lumost: <pre><p>DI doesn&#39;t decouple these layers, it just hides the coupling. The primary difference between the java projects and go projects I&#39;ve been on has been the number of distinct types in use. Java projects tend to require DI as the type system encourages type proliferation, and longer instantiation chains to go with the resultant types.</p></pre>VirmundiUndead: <pre><p>It does decouple the code from the instansiation. If you didn&#39;t pass the DB layer in, you&#39;d always have to run tests against the service layer with the DB on. </p></pre>carsncode: <pre><p>You don&#39;t need a DI framework or IOC container to achieve dependency injection or testable implementations. You can just have to write decent code.</p></pre>VirmundiUndead: <pre><p>I didn&#39;t say you needed them. I said they are useful. You don&#39;t need many of the features of golang, but they are useful. </p></pre>PsyWolf: <pre><p>What&#39;s so terrible about instantiating your class differently in a test? I don&#39;t see how &#34;having two flows&#34; as you put it is a problem. </p> <p>If my normal constructor takes no input, and my test constructor takes a mock db, it&#39;s clear and explicit exactly which external dependencies this class has. With a DI framework, it&#39;s all implicit and hidden away. No thank you. </p></pre>VirmundiUndead: <pre><p>While you&#39;re free to do as you wish, DI through constructors balances magic and practicality. Your dependencies are explicit. The magic of exactly how exists. </p></pre>_ak: <pre><p>I know what DI is for. But that was not my question. Why would I need a framework for that? Decoupling is just using other objects by behaviour (i.e. interfaces), and having the objects resp. the instantiation function provided from the outside. That&#39;s trivial to do yourself without requiring any kind of framework.</p></pre>alasijia: <pre><p>&#34;no needs to use frameworks&#34; means I understand every part of my project. I don&#39;t like DI, this is another reason I don&#39;t like web dev with Java. DI hides too much.</p></pre>carsncode: <pre><p>Nothing like reading through a codebase and having no idea what code will actually execute until you flip back and forth between the actual code and the DI wiring, until you give up and just debug execution to see where it goes. DI frameworks turn everything into &#34;and then something mysterious happens&#34;.</p></pre>tmornini: <pre><p>For me, the things that attract me to Go -vs- Java are:</p> <p>1) SIMPLICITY</p> <p>2) Go&#39;s CSP style concurrency primitives</p> <p>3) Tiny, single file executables</p></pre>AllThatIsMeh: <pre><p>Same here. From my brief experience with go I love the small footprint of it compared to Java. </p></pre>the_web_dev: <pre><p>The big thing for Java is existing libraries and services. Elasticsearch for example is 100% Java and is used by... everybody. There are also a couple B-tier game engines in Java that are probably the best free and open game engines out there. </p></pre>tech_tuna: <pre><p>Lucene, which is the underlying engine behind Elastic and just about any other modern and popular search stack, is 100% Java, as are many other big data tools e.g. Hadoop, Cassandra, Spark, Storm, Kafka.</p> <p>That being said, most of the newer DevOps tools are written in Go: Docker, Kubernetes, pretty much all of Hashicorp&#39;s tools, etc.</p></pre>rco8786: <pre><p>Unless you&#39;re a maintainer of ES the fact that it&#39;s written in Java is inconsequential. </p></pre>mallocc: <pre><p>Java&#39;s concurrent collections are stellar. It would be fantastic to have something similar in Go but probably not terribly practical given lack of generics. </p></pre>VirmundiUndead: <pre><p>I&#39;m a fan of strong typing. Go lacks generics. When you get a collection it&#39;s up to you, the developer, to know what&#39;s in it. This works while the code is small. As more layers or users arrive you have start paying attention to it. </p></pre>jocull: <pre><p>This is something that frequently gets passed off as &#34;not a big deal&#34; by gophers. But then I see code generation, excess boilerplate, unsafe interface casting, and all sorts of hacks to deal with it.</p> <p>Generics are important for static typing safety reasons. Without them, you&#39;re just writing (truths)(interface{})lies.</p></pre>djhworld: <pre><p>Java has</p> <ul> <li>Great tooling</li> <li>Great package management and ecosystem, although you can get yourself into a mess if you&#39;re not careful </li> <li>Great test libraries, Mockito, Hamcrest etc </li> <li>Great ecosystem, good quality libraries (e.g. Guava, CommonsLang etc) </li> <li>Great dependency injection frameworks (e.g. Guice, Dagger etc)<br/></li> <li>Great logging libraries and pluggable architectures (e.g. SLF4J) </li> <li>Great monitoring features (JMX, JVM visualisation/monitoring/stats etc) </li> <li>Generics if you need them, although you&#39;ll probably only really use them for data structures/collections library </li> </ul> <p>Go has the benefit of</p> <ul> <li>Quicker compilation times</li> <li>Less ceremony in getting things going, no need for maven/gradle configuration files etc, no need to learn how Spring works if you&#39;re using Spring and wondering where all the magic happens </li> <li>Readable and less boilerplate code</li> <li>Being great for making command line tools that don&#39;t suck/require you to write special wrapper scripts </li> <li>Lower memory usage (application dependant....)</li> </ul> <p>For things like &#34;Scientific development&#34; you&#39;ll probably find that neither Go or Java are suited, or have suitable community backing - a lot of the scientific programming community seems to have coalesced around Python with things like numpy etc. </p></pre>carsncode: <pre><p>For GUI and game development, the support just isn&#39;t there yet, though it&#39;s not Java&#39;s strong suit in general either; better than go for sure but not great. For servers, workers, and command line tools, Go is excellent - easy to work with, easy to deploy, extremely efficient. In the places where Java excels most, Go offers a lot of advantages. It&#39;s not perfect, so as always, pick the right tool for the job based on research and experimentation.</p></pre>Feribg: <pre><p>Java - big, convoluted language, amazing ecosystem</p> <p>Go - small, concise language (missing some nice stuff like generics), fairly poor ecosystem</p> <p>My 2 cents. For those reasons I typically stick to Java for bigger projects and Go for smaller, fun stuff, or where a lot of concurrency comes in handy.</p></pre>metamatic: <pre><p>Things I miss from Java when writing Go:</p> <ol> <li>Parametric polymorphism.</li> <li>The J2SE collection classes.</li> <li>Good logging via a de facto standard API with pluggable backends (SLF4J).</li> <li>Being able to find libraries for everything.</li> </ol> <p>Things I miss from Go when writing Java:</p> <ol> <li>Not needing an IDE.</li> <li>Ease of deployment.</li> <li>Lower resource usage.</li> <li>Easier parallelism.</li> <li>gofmt.</li> </ol> <p>Things I dislike in both:</p> <ol> <li>Ugly date and time handling APIs (at least pre-JSR310).</li> </ol></pre>c10i: <pre><ol> <li><p>Go has clear separation between data and control logic rather than imposing the OOP-centric paradigm. You don&#39;t often see the Builder/Factory/Manager things in Go, which would tend to make the program more complicated and bloated than necessary.</p></li> <li><p>Go has value type so composite values can be allocated in stack explicitly.</p></li> <li><p>Go generally has smaller memory footprint because of a) no JVM required b) the mentioned in the above 1 and 2 points.</p></li> <li><p>The startup time of Go program is shorter. This might be an exclusive problem that only JVM languages have. </p></li> <li><p>Go is more readable than Java for various reasons. It&#39;s a simpler language after all. And the case-for-visibility is also very consistent among different projects (all Go project actually).</p></li> <li><p>Native concurrency primitives. In Go you have the choice to expose a channel to user code. For most other languages, callbacks or virtual function overriding is the only choice. And they are not as elegant and convenient as channels.</p></li> <li><p>(If you are a Vim user) Go has exceptionally decent Vim support thanks to vim-go. </p></li> </ol></pre>redditbanditking: <pre><p>I used Java professionally for 5 years, was a game developer, before switching to Ruby and now Go doing backend stuff. Here&#39;s my opinion:</p> <blockquote> <p>a) Mobile Development</p> </blockquote> <p>Java for Android still stands strong, unless Google decides to make Go compiler for Android.</p> <blockquote> <p>b) Web Development</p> </blockquote> <p>For frontend no. For backend (including backend of your frontend), yes for Go. I have no extensive experience using J2EE, but I would rather deal with Go backend projects.</p> <blockquote> <p>c) Games Development</p> </blockquote> <p>Between Java and Go, Java still triumphs but mainly because it&#39;s older and more mature with plenty of game development library out there. If Go community can catch up on this, I would imagine Go could become popular among game developers too due to its similarity to C.</p> <blockquote> <p>d) Scientific Development</p> </blockquote> <p>I would say Java. One thing I have yet seen in Go is a mature graphics library with GUI, which I would like to assume is needed for scientific apps.</p> <blockquote> <p>e) GUI Development</p> </blockquote> <p>See above.</p> <p>What I like about Go that I think is better than Java:</p> <ol> <li><p>How Go handles concurrency/channels. Java isn&#39;t too bad, but Go still does it better.</p></li> <li><p>Go interface composition rather than inheritance.</p></li> <li><p>Race condition test with the <code>-race</code> tag.</p></li> <li><p>Many useful packages that come with the standard Go library, like testing, benchmarking. Other languages typically need an external library to do the job.</p></li> <li><p>Single binary, and painless to deploy to servers. Really, if you want to, you could just <code>scp</code> your binary.</p></li> <li><p>If you are building a backend server, no need for additional server layer like Apache, nginx, or unicorn. Go is efficient and fast enough to act on its own.</p></li> </ol> <p>What I dislike about Go, that I think Java handles it better:</p> <ol> <li><p>Tooling around the language. Even to this date, they are still discussing how to handle project dependencies, even though the language was publicly released 5 years ago.</p></li> <li><p>Packages and filenames are loosely defined. It&#39;s basically like structuring your C project like Java. Java is more strict in this regard, which yields more consistent structure.</p></li> <li><p>Dependency on the open source ecosystem. I don&#39;t like how Go forces you to download all your library code. Can&#39;t do static linking. Also, import paths must include repository names, like <code>import &#34;github.com/foo/bar&#34;</code>. No relative pathing. If you call your project <code>myfoo</code>, expect to write <code>import &#34;github.com/myname/myfoo/my/sub/pkgs&#34;</code> all over the place, even from within your own <code>myfoo</code> project. This leads to tight coupling between how your project is stored locally, how it&#39;s stored remotely, and how it&#39;s actually written, which is a big no-no IMO. Forking an OS project lead to a problem as the code will have references to the original source all over the place. People say it&#39;s no big deal and the solution is add another <code>git remote</code>, but when the solution to a compile error problem is using <code>git</code>, I find that quite disturbing. And, should you ever decide to rename your project, you&#39;d be changing imports on <strong>ALL</strong> your files.</p></li> <li><p>Go does better than most languages when it comes to enforcing code format and documentation thanks to <code>go fmt</code>, but Java still hands down the best in this game.</p></li> </ol> <p>TLDR: I like Go better than Java as a language, but the ecosystem and tooling around Go are not quite mature yet.</p></pre>tgaz: <pre><blockquote> <p>Java for Android still stands strong, unless Google decides to make Go compiler for Android.</p> </blockquote> <p>I thought Go had been available for Android for quite a while: <a href="https://godoc.org/golang.org/x/mobile/cmd/gomobile" rel="nofollow">https://godoc.org/golang.org/x/mobile/cmd/gomobile</a>, <a href="https://github.com/golang/go/wiki/Mobile" rel="nofollow">https://github.com/golang/go/wiki/Mobile</a></p> <p>Though a year ago, <a href="/r/golang" rel="nofollow">/r/golang</a> said <a href="https://www.reddit.com/r/golang/comments/40d12d/is_gomobile_ready_for_general_application/" rel="nofollow">it wasn&#39;t very complete</a>.</p> <blockquote> <p>Also, import paths must include repository names, like <code>import &#34;github.com/foo/bar&#34;</code>. No relative pathing.</p> </blockquote> <p>Does Java do relative imports nowadays? (Since the original question was about comparing the languages.)</p> <blockquote> <p>And, should you ever decide to rename your project, you&#39;d be changing imports on ALL your files.</p> </blockquote> <p>That&#39;s true for any Java project that uses more than one package as well (assuming Java still doesn&#39;t do relative imports). <code>com.github.foo.bar</code> is pretty similar to <code>github.com/foo/bar</code>.</p> <blockquote> <p>Go does better than most languages when it comes to enforcing code format and documentation thanks to <code>go fmt</code>, but Java still hands down the best in this game.</p> </blockquote> <p>How does Java do it better?</p></pre>redditbanditking: <pre><blockquote> <p>I thought Go had been available for Android for quite a while: <a href="https://godoc.org/golang.org/x/mobile/cmd/gomobile" rel="nofollow">https://godoc.org/golang.org/x/mobile/cmd/gomobile</a>, <a href="https://github.com/golang/go/wiki/Mobile" rel="nofollow">https://github.com/golang/go/wiki/Mobile</a></p> <p>Though a year ago, <a href="/r/golang" rel="nofollow">/r/golang</a> said it wasn&#39;t very complete.</p> </blockquote> <p>Yeah, I wouldn&#39;t call it complete either just because it can compile things for mobile. I mean it makes sense since Android is Linux and Go just natively supports that, but there&#39;s a lot more going on on mobile development than just compiling a hello world.</p> <blockquote> <p>Does Java do relative imports nowadays? (Since the original question was about comparing the languages.)</p> </blockquote> <p>There&#39;s a misunderstanding what I meant by relative imports. In Go, everything has to be under <code>$GOROOT</code>, which is fine, but so is the import paths. So instead of taking where the current directory is, and add the import paths to it, Go is using the <code>$GOROOT</code> and add the import paths.</p> <p>Java doesn&#39;t dictate you where and how you save your project files. <code>com.github.foo.bar</code> can be located in <code>$ANYTHING/myjavaproject/src/com/github/foo/bar</code>, it can be even on your thumb drive, while in Go it <em>must</em> be located in <code>$GOROOT/src/github.com/foo/bar</code>. All Go engineers out there have the same way of storing their Go project files: <code>$GOROOT/src/..</code> Do you see that the <code>src</code> folder in Java comes <em>after</em> the project folder, and in Go it&#39;s <em>before</em>? So the way Go is seeing all your projects and dependencies, it&#39;s under one big <code>src</code> clumped together. This is why dependency management is a colossal f*up in Go.</p> <p>And to nitpick, while in Java I can use <code>com.whateveriwant</code>, in Go, it has to be where your remote repository is. This is what I meant tight coupling in the way Go works with how you store your project files locally and also how you store it remotely.</p> <blockquote> <p>That&#39;s true for any Java project that uses more than one package as well (assuming Java still doesn&#39;t do relative imports). com.github.foo.bar is pretty similar to github.com/foo/bar.</p> </blockquote> <p>No it&#39;s not. Related above. If I call my Java project <code>testfoo</code> located on <code>/home/redditbanditking/projects/testfoo</code>, I can use <code>com.mycompany</code> as my package name. then I decide to rename <code>testfoo</code> to <code>looksliketherealthingnow</code>, and I don&#39;t need to change <code>com.mycompany</code>. <strong>Big</strong> difference between Java package names and remote repository. In Go, your remote repository <strong>is</strong> your package name.</p> <p>If let&#39;s say Susan is using my library, she does not need to worry about &#34;oh shit, looks like Joe just renamed his repository to looksliketherealthingnow. I have to update my code too then.&#34;. Then Cathy is using Susan&#39;s code, and &#34;oh shit, looks like Susan changed something, oh no it&#39;s Joe doing it.&#34; Oh hey, Tim&#39;s code is using my library too, but he hasn&#39;t been active for a while and hasn&#39;t seen my changes. Cathy is unfortunately using both Susan and Tim, and now she&#39;s stuck between two versions of my code. This leads to a cascading dependency nightmare.</p> <blockquote> <p>How does Java do it better?</p> </blockquote> <p>Javadoc. This may be my own opinion. I prefer structured format in reading documentation. Don&#39;t get me wrong, Go is way better than other languages that most don&#39;t even care about formatting and documentation.</p> <p>EDIT: oh btw, want to see an example of this absurdity that&#39;s Go package names? Docker has been renamed to Moby. Their <a href="https://github.com/docker/docker" rel="nofollow">https://github.com/docker/docker</a> is a redirect to <a href="https://github.com/moby/moby" rel="nofollow">https://github.com/moby/moby</a>. Every. Single. Go project out there that is using <code>github.com/docker/docker</code> is relying on the fact that github.com does a redirect. Once this redirect stops working, it will all turn into chaos.</p></pre>tgaz: <pre><blockquote> <p>then I decide to rename testfoo to looksliketherealthingnow</p> </blockquote> <p>Ah, yes, if you&#39;re talking about <code>GOROOT</code> rather than package name, agreed.</p> <p>I would like to draw parallels to what e.g. Maven does in terms of (more or less) dictating a directory structure, but I don&#39;t know if people use other tools now.</p> <blockquote> <p>Javadoc</p> </blockquote> <p>Thanks for the example. I definitely agree godoc is harder to read.</p></pre>goomba_gibbon: <pre><p><a href="http://i.imgur.com/iE1spq1.jpg" rel="nofollow">http://i.imgur.com/iE1spq1.jpg</a></p></pre>sheepiroth: <pre><p>One thing java does better:</p> <p><code>/com/foobar/java/android/project/app/android/src/foobar/Foobar.java</code></p> <p>but in all seriousness, I use go A LOT for very simple portable components that require like ~200 lines of code. No JVM required, everything compiled into a portable blob, it&#39;s like a dream come true for microservices.</p> <p>That isn&#39;t to say go can&#39;t be used for big projects or that Java should <em>never</em> be used for small components. I find that a huge strength of go is how it appears and feels simple and intuitive because of the amazing breadth of the standard library in go.</p> <p>Java has its own advantages that have been covered elsewhere in the thread, the most important in my opinion being the maturity and network effect some of its libraries come with. There isn&#39;t much that can replace a long history of a community of talented individuals contributing code and knowledge to a project.</p></pre>toula_from_fat_pizza: <pre><p>Java garbage collection is possibly the worst thing I have encountered in all my years of development.</p></pre>pinpinbo: <pre><p>There&#39;s one small thing that bugs me about Java.</p> <p>Why am I not allowed to use regular function and closure? Why does everything has to be an object? This changed slightly since Java 8&#39;s lambda is introduced, but it is not a true closure.</p></pre>hygutyughugug: <pre><p>java can do them all. people cry about its verbosity but 20 years of java programming has made it a non-issue for me.</p> <p>Go can do most of those(with the right bindings).</p> <p>I can easily find work in java. Go is a bit tougher. With Go I have a hard time finding work outside of the bay, or new york, or denver areas.</p> <p>However, my highest paying project to date was done in Go.</p> <p>Clearly, java has the edge, and will for some time.</p> <p>Edit: it is okay, down vote the factual but unflattering comment without retort.</p></pre>thomascgalvin: <pre><p>I think Java&#39;s package management is better than any major programming language out there. Maven can be a pain in the ass to work with sometimes, but it makes pretty much anything you want to do in regards to packaging at least possible.</p> <p><code>go get</code> is super simple, but the fact that it requires all code to be backwards compatible forever is just ... dumb. Reproducible builds are super important in large scale applications, and that&#39;s impossible when your packaging tool just grabs <code>latest</code>. I cannot fathom when semver wasn&#39;t the default from day one.</p> <p><a href="https://github.com/golang/dep" rel="nofollow"><code>dep</code></a> should fix a lot of this.</p> <p>On the other hand, Java&#39;s habit of littering your filesystem with JARs is a pain in the ass, and the fact that when a go build is done you can just say &#34;here&#39;s your executable&#34; is really nice. Trying to build a &#34;double click to run this program&#34; in Java is vastly more complicated than it needs to be, especially is one of your libraries has had fuckery performed on its manifest file.</p></pre>google_you: <pre><p>Go is old Java, simple, no generics, has garbage collection. It&#39;s better at brining nostalgia than modern Java.</p></pre>karma_vacuum123: <pre><p>Java actually makes use of its interface feature</p> <p>The Go SDK has very few interesting and useful interface definitions...it&#39;s almost as if the SDK developers don&#39;t have much use for interfaces at all beyond <code>io</code></p> <p>Also, Java has real attributes so it doesn&#39;t need out-of-language hacks like structure tags</p></pre>carsncode: <pre><p>Given the way that interfaces work in Go, having a bunch of interfaces in the standard library is unnecessary. If you want to refer to something I&#39;m the standard library by an interface, you can just write the interface you need and the standard library implements it implicitly.</p> <p>Struct tags aren&#39;t hacks, they&#39;re part of the language, so I&#39;m not really sure what you mean there.</p></pre>snippet2: <pre><p>Yeah I had a guy say defer is a hack too. Idk where this&#34;hack&#34; thing is on the web but apparently they have access to it lol.</p> <p>Another thing people forget about generics is that it&#39;s where a language draws a line in the sand for versioning. They like never worked in a company that uses them on a project that has to always be updated.</p></pre>karma_vacuum123: <pre><p>the fact that struct tags are strings is in the spec</p> <p>what is IN the tags themselves is not in the spec. many libs inject arbitrary instructions into tags.</p> <p>as for having more interfaces in the SDK...of course not required, but would help focus the community. don&#39;t you think <code>io.Reader</code> is valuable?</p></pre>carsncode: <pre><p>That&#39;s true, what&#39;s in tags isn&#39;t in the spec, it&#39;s a language feature. The entire value is that you can use it as you see fit. What&#39;s in a struct or a string or a function isn&#39;t in the spec either, you can put whatever you need to into them to suit your purposes. That&#39;s the whole point of a programming language.</p> <p>The interfaces in the standard library are certainly useful, and I never suggested they aren&#39;t. I only said there is no reason to have extra interfaces, because you can define your own and use them with standard library types with Go&#39;s implicit interface implementation.</p></pre>

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

740 次点击  
加入收藏 微博
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传