Why Go instead of C++?

blov · · 510 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>I have programmed C++ many years and like it. Contributing to someone else&#39;s Go program, I am now learning Go.</p> <p>Go seems fine. I can see that many like it, but I do not yet grasp its appeal. Can you explain to me why Go is interesting?</p> <p>Go and C++ seem to differ in garbage collection, stack/heap management, RAII, reference counting, exception handling, concurrency facilities, modularization and (reputedly) compile time, perhaps among others; but the differences -- though indeed different -- have not yet added up to any compelling advantage for Go in my understanding.</p> <p>I do not criticize Go. I am only trying to understand. Since I am now spending some time coding Go, it would help if I grasped the relative appeal or motive of this language.</p> <hr/>**评论:**<br/><br/>hayzeus: <pre><p>The upside of Go is garbage collection, an actual concurrency model and language simplicity (I have about 20 years of C++).</p> <p>The downside of Go might be garbage collection, depending on your application. For example, I wouldn&#39;t use it for anything but the softest of real time applications, and its really out for any kind of systems programming. All in all for raw computing power, well-written go will generally be slower than well-written C++.</p> <p>But for, say, web services? You&#39;d be nuts to write that kind of thing in C++.</p> <p>Also I came to C++ from C and really missed the elegance of C. Go recaptures some of that elegance. This is really more just aesthetic, though.</p></pre>weberc2: <pre><p>I would also add:</p> <ul> <li>Go has reflection out of the box--this is a useful property for things like serialization/deserialization (I understand that C++ may have recently added some form of reflection, so this point may be less valid or invalid altogether)</li> <li>Go&#39;s standard library is much more comprehensive</li> <li>Go&#39;s build system is much more convenient</li> <li>Go&#39;s tooling is much nicer <ul> <li>More readable compiler errors</li> <li>There are no suitable C++ analogs for vim-go, and even getting something as straightforward as autocomplete is painful</li> </ul></li> </ul></pre>jussij: <pre><p>I would also add:</p> <ul> <li>No more header files</li> <li>No more worrying about weird linker errors</li> </ul></pre>metamatic: <pre><ul> <li>Much faster compile times.</li> <li>There&#39;s actually a standard way to build your application without needing additional tools with their own languages.</li> <li>Cross-compilation and deployment are much easier.</li> <li>Language doesn&#39;t take &gt;5 years to learn and be proficient in.</li> <li>Fewer language pitfalls. One of the reasons why it takes so long to be proficient in C++ is that you need to learn all the things to avoid doing. Even once you &#34;know&#34; the language, you&#39;ve still got <a href="https://play.google.com/store/books/details/Scott_Meyers_Effective_C_Digital_Collection?id=U7lTySXdFk0C&amp;hl=en" rel="nofollow">hundreds of things to learn</a>.</li> </ul></pre>thepciet: <pre><blockquote> <p>No more header files</p> </blockquote> <p>I miss them for sure, but not worth the compiler implementation leakage into the programmer&#39;s world.</p> <p>Specifically there&#39;s something neat about a well described &#34;thing in a computer&#34; contained in a text file that fits on a screen. I&#39;m not comfortable here in Go yet as readable code uses different patterns than in header languages.</p></pre>natefinch: <pre><p>This is what godoc is for... it gives you what you&#39;d get in the header file.</p></pre>exasperation: <pre><blockquote> <p>This is really more just aesthetic, though.</p> </blockquote> <p>I think it&#39;s than just aesthetics. The amount you need to learn to read arbitrary Go code is nowhere near as large as you need to learn to read arbitrary C++ programs. </p> <p>The C++ standard is so large and supports so many styles of programming that at the extreme ends of styles, the &#34;dialects&#34; aren&#39;t really mutually intelligible. </p> <p>It is also a moving, ever-changing target. I knew C++98 quite well, but C++11 is like a whole other language!</p> <p>These are tremendous potential strengths, of course. <em>If</em> you have experienced and skilled C++ programmers, an organized engineering team, a software architect and management willing to impose a standard coding convention on the team. </p> <p>For inexperienced programmers, for loose teams, for long-term maintenance by those, it&#39;s easy to get sucked into a complexity trap where different architectures are glued together inappropriately or turns into spaghetti.</p></pre>SandalsMan: <pre><p>I gave up on C++ after seeing things like this: <code> template &lt;typename T&gt; inline T const&amp; Max (T const&amp; a, T const&amp; b) { return a &lt; b ? b:a; } </code></p> <p>I somewhat understand what this is doing but there is just so much to know here.</p></pre>kl0nos: <pre><p>Rust is even worse.</p></pre>s-expression: <pre><p>Rust is much better:</p> <pre><code>fn max&lt;T&gt;(x: T, y: T) -&gt; T { if x &gt;= y { x } else { y } } </code></pre></pre>kl0nos: <pre><p>You are joking right? look at this:</p> <pre><code> fn parse_request&lt;&#39;a&gt;(bytes: &amp;&#39;a [u8]) -&gt; IResult&lt;&amp;&#39;a [u8], TrackerRequest&lt;&#39;a&gt;&gt; { switch!(bytes, tuple!(be_u64, be_u32, be_u32), (CONNECT_ID_PROTOCOL_ID, ::CONNECT_ACTION_ID, tid) =&gt; value!( TrackerRequest::new(CONNECT_ID_PROTOCOL_ID, tid, RequestType::Connect) ) | (cid, ::ANNOUNCE_IPV4_ACTION_ID, tid) =&gt; map!(call!(AnnounceRequest::from_bytes_v4), |ann_req| { TrackerRequest::new(cid, tid, RequestType::Announce(ann_req)) }) | (cid, ::SCRAPE_ACTION_ID, tid) =&gt; map!(call!(ScrapeRequest::from_bytes), |scr_req| { TrackerRequest::new(cid, tid, RequestType::Scrape(scr_req)) }) | (cid, ::ANNOUNCE_IPV6_ACTION_ID, tid) =&gt; map!(call!(AnnounceRequest::from_bytes_v6), |ann_req| { TrackerRequest::new(cid, tid, RequestType::Announce(ann_req)) }) ) </code></pre> <p>}</p></pre>s-expression: <pre><p>I&#39;m not arguing that Rust is a clean language. I&#39;m just saying that generics in Rust are a lot cleaner than generics in C++.</p></pre>FUZxxl: <pre><p>Except now I have to add a shitload of useless braces.</p></pre>s-expression: <pre><p>Yeah the lack of ternary in Rust makes this comparison a bit lame.</p> <p>Though not having to sprinkle <code>const</code> everywhere is nice.</p></pre>mauroPic2: <pre><p>I think than this is a misconception...many languages are readables after a while...for instance, lisp languages are difficult to read at first instance but after a time you can find this more simple to use and read than any other language</p> <p>sorry but deflate a language because it doesnt look nice at first is a newbie mistake</p></pre>jussij: <pre><blockquote> <p>many languages are readable after a while</p> </blockquote> <p>Over the years I&#39;ve coded in dozens of languages, including C++, but I&#39;ve seen template code like that and it always leaves me confused :(</p> <p>Why is it so confusing?</p> <p>Consider the line of template code presented:</p> <pre><code>template &lt;typename T&gt; inline T const&amp; Max(T const&amp; a, T const&amp; b) { return a &lt; b ? b:a; } </code></pre> <p>What the hell is this preamble trying to say:</p> <pre><code>template &lt;typename T&gt; </code></pre> <p>Correct me if I am wrong but is that line of code only trying to describe is this action:</p> <pre><code>const&amp; T Max(T const&amp; a, T const&amp; b) { return a &lt; b ? b:a; } </code></pre> <p>If not, what is the difference in those to lines of code?</p> <p><strong>EDIT:</strong> As a comparison C# would use a syntax <em>similar</em> to the following which I think is a lot easier to read and understand:</p> <pre><code>const&amp; T Max&lt;T&gt;(T const&amp; a, T const&amp; b) { return a &lt; b ? b:a; } </code></pre> <p>I spent decades coding C++ but these days I spend much more time coding C# and I find C# code is so much easier to read :(</p></pre>metamatic: <pre><p>That&#39;s exactly why the Go team are so wary of adding generics to Go. You might end up with horrifically complicated syntax like C++ and Java.</p> <p>Also, you might end up with accidentally turing-complete compilations, like C++ <a href="https://arxiv.org/abs/1605.05274" rel="nofollow">and Java</a>, meaning your compile times are <a href="http://cpptruths.blogspot.com/2005/11/c-templates-are-turing-complete.html" rel="nofollow">essentially unbounded</a>.</p></pre>: <pre><p>[deleted]</p></pre>TheMerovius: <pre><blockquote> <p>C++ templates and Java generics are not the same thing.</p> </blockquote> <p>That wasn&#39;t claimed by anyone. The claim was that a) both use &#34;horrifically complicated syntax&#34; and b) both are Turing complete. Both are true, to the best of my knowledge.</p></pre>boomshroom: <pre><p>That&#39;s why most gophers don&#39;t use go generate for generics, instead what they do is <em>not use generics</em>, just like what has been done for decades in C.</p></pre>s-expression: <pre><blockquote> <p>they do is not use generics, just like what has been done for decades in C.</p> </blockquote> <p>This is not true. People use ad-hoc generics in C <em>all the time</em>. They just eschew type safety and make a big ball of crap using <code>void *</code> and <code>union</code></p> <p>Just like Go programmers do with throwing <code>interface{}</code> everywhere</p></pre>KingCepheus: <pre><p>Throwing <code>interface{}</code> everywhere is not idiomatic Go.</p></pre>s-expression: <pre><p>It might not be considered idiomatic, but it sure is <em>everywhere</em>. Even the standard library uses <code>interface{}</code> as a poor-man&#39;s generic in many places.</p></pre>natefinch: <pre><p>Most gophers avoid interface{} like the plague. There are a few packages in the stdlib that use interface{} heavily - some are things like json, which need to be able to encode any value... thus interface{} is actually more or less correct. And some are things like the container packages that probably most people on the Go team wish hadn&#39;t ever been added to the stdlib. Certainly I&#39;ve never see anyone actually use those ugly container libraries.</p> <p>So, I wouldn&#39;t say it&#39;s all over. In fact, interface{} is very rarely used, and always scrutinized heavily when it is proposed, in my experience.</p></pre>KingCepheus: <pre><p><code>net.Context</code> and <code>sync.Pool</code>, and the <code>json</code> package are the only places I remember using <code>interface{}</code>. I don&#39;t consider that &#34;everywhere.&#34;</p></pre>hayzeus: <pre><p>Yeah -- I worded that poorly. Obviously elegance is important. But for me the difference is mainly aesthetic (because I&#39;ve had a lot of C++ in my past). </p></pre>IndianAlien: <pre><blockquote> <p>spaghetti</p> </blockquote> <p>Seems like the majority of C++ codebases.</p></pre>Partageons: <pre><blockquote> <p>well-written go will generally be slower than well-written C++.</p> </blockquote> <p><a href="http://benchmarksgame.alioth.debian.org/u64q/compare.php?lang=go&amp;lang2=gpp" rel="nofollow">Benchmark confirms.</a></p></pre>kl0nos: <pre><p>Anyone know why Go b-tree implementation is so slow in this benchmarks?</p></pre>FUZxxl: <pre><p>Because it&#39;s a benchmark for the garbage collector which C++ doesn&#39;t have. The C++ example uses a custom allocator for the tree&#39;s nodes (cf. <code>NodePool</code>), further speeding up the program. I wouldn&#39;t say that the performance of the C++ implementation is typical for C++, obviously, a lot of thought went into it, way more than what goes into a typical C++ program.</p></pre>hayzeus: <pre><p>Also worth noting that regexes in go are very slow -- and this will be reflected in the benchmarks that use regexes (dna, etc)</p></pre>tmornini: <pre><p>And will also be fixed eventually.</p></pre>Partageons: <pre><p>Go is already <a href="http://benchmarksgame.alioth.debian.org/u64q/compare.php?lang=go&amp;lang2=java" rel="nofollow">on par with Java</a> and it&#39;s only going to get better.</p></pre>tmornini: <pre><p>Except for the regex tests...</p></pre>Partageons: <pre><p>The Go FAQ provides excuses for <code>regex-dna</code> and <code>pidigits</code> benchmarks:</p> <blockquote> <p>The slowest depend on libraries for which versions of comparable performance are not available in Go. For instance, pidigits.go depends on a multi-precision math package, and the C versions, unlike Go&#39;s, use GMP (which is written in optimized assembler). Benchmarks that depend on regular expressions (regex-dna.go, for instance) are essentially comparing Go&#39;s native regexp package to mature, highly optimized regular expression libraries like PCRE.</p> </blockquote> <p><a href="http://benchmarksgame.alioth.debian.org/u64q/performance.php?test=binarytrees" rel="nofollow"><code>binary-trees</code></a>, on the other hand...shame on Go. We&#39;re beaten by the likes of Erlang, Haskell, and Clojure.</p></pre>tmornini: <pre><p>Go is my future.</p> <p>The gap between Go and the others will narrow, though it&#39;s currently plenty close for my uses.</p> <p>And my point is simply that Go&#39;s regex libraries will improve -- and narrow the gap.</p></pre>igouy: <pre><blockquote> <p>The Go FAQ provides excuses…</p> </blockquote> <p>Maybe that&#39;s out-of-date information.</p> <blockquote> <p>… pidigits.go depends on a multi-precision math package, and the C versions, unlike Go&#39;s, use GMP… </p> </blockquote> <p>One of those pidigits Go programs <a href="http://benchmarksgame.alioth.debian.org/u64q/program.php?test=pidigits&amp;lang=go&amp;id=1" rel="nofollow"><strong>does</strong> use GMP</a>.</p> <blockquote> <p>… comparing Go&#39;s native regexp package to mature, highly optimized regular expression libraries like PCRE.</p> </blockquote> <p>One of those regexdna Go programs <a href="http://benchmarksgame.alioth.debian.org/u64q/program.php?test=regexdna&amp;lang=go&amp;id=2" rel="nofollow"><strong>does</strong> use PCRE</a> (and has since 2012).</p> <blockquote> <p>the likes of Erlang, Haskell, and Clojure</p> </blockquote> <p>Maybe note the memory-use for those.</p></pre>FUZxxl: <pre><p>Go regexes are quite fast, the problem is that the benchmarks aren&#39;t using regular expressions, so they have to use the PCRE bindings in order to get the not-so-regular expressions required by the specification.</p></pre>hayzeus: <pre><p>This hasn&#39;t been my experience at all -- but then I&#39;m still using Go 1.5. Hopefully 1.6, 1.7 (I&#39;ll probably upgrade when 1.7 is out of beta) have improved. </p></pre>FUZxxl: <pre><p>What package have you been using?</p></pre>hayzeus: <pre><p>regexp.</p></pre>metamatic: <pre><p>Because it hasn&#39;t had 25 years of optimization work.</p></pre>egonelbre: <pre><p>I guess the most interesting part about Go is that it&#39;s <a href="https://www.youtube.com/watch?v=4Dr8FXs9aJM" rel="nofollow">not that interesting</a> :D.</p> <p>However more seriously, after several years of considering, the best feature of Go is how packages work. This might look like a small thing how you import and declare packages, but it has a large impact how you write code and how good code looks. Few important notions are: 1. you rarely use a single thing from a package (namespace), hence it makes more sense to import a package (namespace), 2. if you import a package (namespace) you don&#39;t need to include the package-name in the declarations, 3. this encourages packages related to core values (e.g. for web application &#34;user&#34;, &#34;page&#34;, &#34;admin&#34;, &#34;billing&#34;), 4. core values are more stable than a particular implementation, 5. this means the purpose of the program is more visible from the code rather than hidden in accidental complexity. Basically the way packages work guide you to build better software and you get it more right from the first time. The way Go packages work have impacted my code writing more than any other language (and this includes Delphi, C, C++, D, Java, C#, Pascal, JavaScript, Prolog, Haskell, ASP, Lisp, Clojure, APL, Smalltalk ... and probably some that I forgot).</p> <p>The second best feature is that it <a href="https://golang.org/ref/spec" rel="nofollow">fits in your head</a> -- i.e. the complexity of the language has never gotten in my way. This means that the language mostly disappears when you write code and you are left only with the problem on hand (obviously, not for all problems).</p> <p>Of course there are nice tooling, great packages, language feels syntactically light... basically what other have said.</p> <p>These features aren&#39;t exclusive, but you need to work to get them in other languages. For the first one, you would need to start to structure code in a way that may not be necessarily idiomatic in that language. In the second you need to pick some particular subset of a language.</p></pre>Logothson: <pre><p>You mention packages. I have never understood why C++ lacks proper support for packages. The conventional explanation is that &#34;Inclusion guards work,&#34; but is this really an explanation? Maybe they do work, but they are just awkward -- awkward in a way other C++ features are not. Inclusion guards don&#39;t feel right -- and not only to me, but to lots of C++ programmers.</p> <p>A C++ header is a purely <em>textual</em> device, used for a largely <em>logical</em> purpose. It seems to me that C++ ought to do a little better than that.</p> <p>Nevertheless, I admit, inclusion guards do seem to work, so Go&#39;s improved packaging alone would not have sufficed to entice me to convert.</p> <p>Syntactical lightness is important. I have programmed C++ long enough that I can actually read it, but one empathizes with the programmer who has spent a year or two coding C++ and still usually cannot read code others have written.</p> <p>Thank you for your informative reply.</p></pre>egonelbre: <pre><p>C++ doesn&#39;t have modules because C didn&#39;t have modules. But it has been proposed several times (e.g. <a href="http://www.open-std.org/JTC1/SC22/WG21/docs/papers/2015/n4465.pdf" rel="nofollow">one of the latest</a>), so maybe one day. <code>#include</code> was used to work around the limitation of not having proper modules/packages, and it just stuck around and there are <a href="https://www.bell-labs.com/usr/dmr/www/chist.html" rel="nofollow">historical</a> reasons why <code>#include</code> was introduced.</p> <p>Do note, it&#39;s not just &#34;the grouping of things&#34; in packages, it&#39;s also how they work together with other features and how the code evolves due to them. The feature-set guides to better code. It&#39;s really hard to explain... </p> <p>I&#39;ll try to give a similar analog from C++, although negative. I suspect you have come across template nightmares and I&#39;m not talking about the error messages. I mean once you introduce a template, it seems to infect other code near it with templates -- i.e. the human instinct is to keep template parameters around. Essentially you start with a small template and finally end up with a lot of type parameters everywhere reducing readability (e.g. instead of <code>Vector&lt;Item&gt;</code> you could use <code>Items</code> or instead of making an <code>Set&lt;Item&gt;</code> with tons of methods, you could make just a simple <code>ActiveItems</code> with few methods). This is a case where the feature-set guides you to keep abstraction around, which ends up reducing readability. <em>I&#39;m not sure whether everyone has that problem, but I&#39;ve seen that happen several times. (Just in case, I&#39;m not arguing against templates.)</em></p> <p>Go feature set has an infectious positive effect, somehow the instinct to remove repetition in names, not allowed to have circular imports, having implicit interfaces, multiple return parameters, no overloading, overriding/inheritance and no generics ends up causing to write clearer code (in the average case).</p></pre>nicerobot: <pre><p>My number one reason: its simplicity. I underestimated how big a deal it is. Then, after a few months writing nothing but Go, it hit me. I realized I wasn&#39;t having to think much about the language when coding. Unless you&#39;ve experienced it, I think you might not fully grasp the significance of design simplicity.</p> <p>I love languages and language design and I can be productive in C, Java, Scala, JavaScript, and Python and have read specs and toyed with dozens of other languages over the years, including C++. I don&#39;t particularly care about language wars. When it gets down to it, use whatever you like and are comfortable using or which have the best libraries/frameworks you need. Scala is fascinatingly academic and is still my go-to for running on the JVM. Java is a fine workhorse. Haskell can be fun and useful to force a paradigm. Lisp/Scheme provide a similarly powerful simplicity to Go but unfortunately few actually use them. I&#39;ve mostly given up on JavaScript&#39;s idiosyncrasies because of Go. And I now always consider Go before I type any Python.</p> <p>Don&#39;t think of Go as a feature-wise competitor to C++, nor anything else. I think of Go as freeing my mind from thinking about how to write a program to solve a problem. Instead, I spend more time thinking about how to solve the problem. That was my epiphany of simplicity.</p></pre>FUZxxl: <pre><blockquote> <p>Don&#39;t think of Go as a feature-wise competitor to C++, nor anything else. I think of Go as freeing my mind from thinking about how to write a program to solve a problem. Instead, I spend more time thinking about how to solve the problem. That was my epiphany of simplicity.</p> </blockquote> <p>That&#39;s a very good point. I feel the same way when writing programs in C.</p></pre>nicerobot: <pre><p>:+1: I didn&#39;t mention it in my comment but I completely agree. C is a model of simplicity and I think the Go team did a fantastic job designing an &#34;improved C&#34;. Not saying Go can nor should replace C, just that it&#39;s an improvement in many respects and without becoming a beast.</p></pre>IndianAlien: <pre><blockquote> <p>And I now always consider Go before I type any Python.</p> </blockquote> <p>Pun intended? </p></pre>natefinch: <pre><p>A bug in your C++ program has the potential to allow callers to execute arbitrary code on your machine. Such a bug is generally impossible in Go.</p> <p>In the small, I can tell you what any line of go code does, the amount of memory it&#39;s going to allocate, the general complexity of it, etc. This is impossible in C++. A simple line like <code>a = b</code> could go download a docker image from the internet.</p> <blockquote> <p>Go and C++ seem to differ in ... (reputedly) compile time</p> </blockquote> <p>What?! Go compile mongodb, I&#39;ll wait. Actually, I won&#39;t. It&#39;s a project approximately on the scale of Juju (they both have ~750,000 lines of code plus or minus depending on how you count). Mongodb, written in C++, takes 45 minutes for me to compile on my machine. Juju takes about 8 seconds. That&#39;s THREE orders of magnitude.</p></pre>FUZxxl: <pre><blockquote> <p>Such a bug is generally impossible in Go.</p> </blockquote> <p>No. This can happen if you use the <code>unsafe</code> package or if there is a compiler bug or if you use cgo.</p></pre>natefinch: <pre><p>That&#39;s pretty much what I meant by generally. </p> <p>unsafe is (surprise!) unsafe. cgo is C and thus unsafe. Compiler bugs... meh, I suppose - I&#39;m not too worried about that.</p> <p>What I was actually thinking of was race conditions that let you do bad things with partially written memory. This is one of the few known potential problems in Go (aside from things easily avoided, like unsafe and cgo).</p></pre>The_Jare: <pre><p>Easy to learn in its entirety, few or no gotchas, tooling, built-in concurrency, comprehensive yet easy to use standard library. </p></pre>Sir_Harry_of_Kane: <pre><p>Just like to point out that anyone with 10+ years of C++ is largely outside the target demo of go. </p> <p>One of go&#39;s biggest advantages is how easy it is to learn. </p></pre>Redundancy_: <pre><p>goroutines as a standard unit of concurrency.</p> <p>I love being able to write something that doesn&#39;t resemble a message handling main loop and to be easy take advantage of free cycles while calls are blocked.</p></pre>dgryski: <pre><p>talks.golang.org/2012/splash.article should answer a number of your questions.</p></pre>newimprovedoriginal: <pre><p>In the scenario where both Go, and C++ are unfamiliar to a developer, is Go easier to install, write a basic web server, compile, and run?</p> <p>Aside from the GO_PATH GO_ROOT confusion of earlier versions, I found it very easy to get something up and running and could see the path forward. </p> <p>With some other languages &amp; stacks I&#39;ve gotten lost in setup, and wasn&#39;t able to see a clear path forward.</p> <p>Don&#39;t underestimate the power of simple &amp; clean documentation.</p></pre>storm14k: <pre><p>That&#39;s been one of the biggest draws to me. There were many times especially with Java that I got so lost in setup and library entanglements that I lost interest in what I was working on. I like being able to open a plain text file and hammer out something in the direction of my solution. I fell in love with Python for this and then Go. In fact I didn&#39;t actually like Go early on until I learned Python and understood the beauty of both.</p></pre>Fwippy: <pre><p>In case this isn&#39;t a rhetorical question, yes.</p> <p>Edit: Whoops, answered before I saw your edit.</p></pre>kaeshiwaza: <pre><p>Simplicity</p></pre>thepciet: <pre><p>After writing Bash, C, Java, Objective-C, some styles of assembly, some proprietary logic programming languages/interfaces, and seeing how network and database applications ideally solve many problems, and then deciding to make a game, I wrote an alpha game application in C++ by linking into the Ogre3D free and open source game engine.</p> <p>I wrote a bunch of CMake after getting through just building and linking Ogre, and then a local C++ chess-like application with the engine.</p> <p>Watching Rob Pike talk about build times and gofmt on youtube was enough for me. I&#39;m currently working with Go for login/db/website/game logic (which can look very C-like) and calling into povray, which is a C++ command line application.</p></pre>int32_t: <pre><p>Some random thoughts:</p> <ol> <li><p>Go is very productive, close to the productivity of dynamic scripting languages. C++&#39;s focused strength is certainly not in this regard. Also, I think compiler speed is one of the contributing factor of the productivity gap.</p></li> <li><p>Compared with most scripting languages, Go is capable of taming the code complexity of large system gracefully. In other words, it&#39;s designed to scale over the size of the system, and the size of people working on it.</p></li> <li><p>The <em>whole</em> system of a Go program is typically very penetrable. You can trace the code down to the bottom of the stdlib or even the compiler builtins in seconds with ease (thanks to the excellent Guru and vim-go). In my experiences of C++, I doubt how many C++ programmers have ever had a look to the implementation of STL or iostream, which is hard to do ergonomically. Although some people occasionally call the opaqueness <em>encapsulation</em> and see it a good practice (which I don agree). </p></li> <li><p>As stated above, the difficulty of static analysis of C++ has something to do with its freedom and complexity, which would prevent such useful analysis tools like Guru from happening. The closet thing in C++ world I can think of still needs building indexes before hand, which can be very time consuming for large code base, and is not so accurate if the code does some tricky things.</p></li> <li><p>The overall coding styles are much more unified and consistent in Go ecosystem, as there is no <em>that</em> much choices. In C++, every library, framework and even application could be like having their own DSL as the best possible abstraction.</p></li> <li><p>Following the above, integration of many third-party libraries in C++ is no as easy as Go. Their build systems are very likely to be different (CMake, Ninja, Makefile, autoconf ... to name a few). Much effort has to be spent in those housekeeping works.</p></li> </ol></pre>s-expression: <pre><h1></h1> <h1>Go</h1> <p>Go is a language optimized for productivity amongst larger teams.</p> <p>This has several interesting implications:</p> <p>1) The language itself is very simple, from syntax and grammar to features and conventions. This means you can become effective with Go in a short amount of time.</p> <p>2) The opinionated decisions made by the language designers resulted in a language in which it&#39;s very difficult to write non-idiomatic code. This means that you can easily read and maintain software written by others.</p> <p>3) It has one of the most complete and useful standard libraries of any language, which is very impressive considering its young age. This means you aren&#39;t hunting down third-party libs to perform essential tasks.</p> <p>4) In the cases where you <em>are</em> using third-party libs, it&#39;s very painless (extension of point 2)</p> <p>On top of that, it has excellent tooling and a very good deployment story.</p> <h1></h1> <h1>C++</h1> <p>C++ is a language optimized for nothing in particular. It&#39;s the culmination of 33 years of seemingly random additions to the C language, though it never removed any of C&#39;s shortcomings.</p> <p>The language is intended for people who &#34;know what they are doing&#34; and are comfortable fighting with compilers to produce the perfect code for the problem at hand.</p> <p>C++ is rarely the most appropriate or pragmatic language for a problem. However in areas where it is appropriate, it&#39;s basically the only sane option available. See: AAA games.</p> <p>C++&#39;s tooling is atrocious, but it&#39;s gotten a little better since the introduction of LLVM/Clang. This is mostly due to the insane complexity of C++&#39;s syntax and grammar which makes writing reliable code comprehension tools very difficult. One must ask himself, if the task is so difficult for computers then what makes you think it would be simple for developers?</p> <p>I don&#39;t particularly care for C++, though I&#39;ve worked with it daily for the past 8 years. I don&#39;t believe that to be coincidental.</p> <p>To the pragmatic developer, the problem domains of Go and C++ do not overlap. It&#39;s like comparing Go and JavaScript.</p></pre>TheMerovius: <pre><blockquote> <p>but the differences -- though indeed different -- have not yet added up to any compelling advantage for Go in my understanding.</p> </blockquote> <p>Humz. Really? You just named everything that makes programming in C++ completely suck and said that go doesn&#39;t have it (oh, also: go doesn&#39;t have a lot of things, as opposed to C++, which is such a moloch of a language, that I&#39;d legitimately claim that no one actually knows it).</p> <p>I don&#39;t know. If you can&#39;t see the appeal in all those things, than maybe you are well-served with C++?</p></pre>

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

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