TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Mistakes C/C++ Devs make writing Go

202 pointsby beyangover 6 years ago

10 comments

ktpsnsover 6 years ago
Here&#x27;s a newbie&#x27;s bit off topic question: As a C++ dev who wants to write &quot;slightly less low-level code&quot;, should I go for RUST or GO? My intuition says Rust is more like a safer version of C++ while Go is more half way to Python&#x2F;Julia. Do you agree with this sloppy assessment?<p>(Please, don&#x27;t start a religious war on programming languages. We are all grown ups)
评论 #17946907 未加载
评论 #17946189 未加载
评论 #17946206 未加载
评论 #17947677 未加载
评论 #17946774 未加载
评论 #17946407 未加载
评论 #17946396 未加载
评论 #17946575 未加载
评论 #17946133 未加载
评论 #17948262 未加载
评论 #17947143 未加载
评论 #17947291 未加载
评论 #17946913 未加载
评论 #17946636 未加载
shooover 6 years ago
re: &quot;# channels &lt; # goroutines&quot;<p><pre><code> func doSomethingTwice() error { &#x2F;&#x2F; Issue occurs below errc := make(chan error) go func() { defer fmt.Println(&quot;done wth a&quot;) errc &lt;- doSomething(&quot;a&quot;) }() go func() { defer fmt.Println(&quot;done with b&quot;) errc &lt;- doSomething(&quot;b&quot;) }() err := &lt;-errc return err } </code></pre> &gt; When one routine writes to the channel, the program exits and the other goroutine is lost, building up memory use as a results<p>I&#x27;m not very familiar with go, but my guess would be that since the two goroutines send to an unbuffered channel that is read at most once, the &quot;slower&quot; of the two goroutines will sit there blocking attempting to send to a channel that will never be read. So this would &quot;leak&quot; a goroutine that would consume resources while the process was still running, but it doesn&#x27;t have anything to do with the program exiting.<p><a href="https:&#x2F;&#x2F;golang.org&#x2F;ref&#x2F;spec#Channel_types" rel="nofollow">https:&#x2F;&#x2F;golang.org&#x2F;ref&#x2F;spec#Channel_types</a><p><a href="https:&#x2F;&#x2F;golang.org&#x2F;ref&#x2F;spec#Send_statements" rel="nofollow">https:&#x2F;&#x2F;golang.org&#x2F;ref&#x2F;spec#Send_statements</a><p>&gt; How do we fix this? We simply increase the number of channels to 2<p>The fix is okay but the language is a bit hazy, there&#x27;s still only one channel, but now it&#x27;s a buffered channel with capacity to hold up to 2 messages, so the two goroutines don&#x27;t need to block waiting for a receiver to be ready to synchronously receive the message they are sending.
评论 #17944898 未加载
评论 #17944749 未加载
评论 #17947177 未加载
评论 #17946797 未加载
agallegoover 6 years ago
This has no c++ content. only C. the mistakes highlighted are just not representative of a team of C devs. the resource leak is specially a bad example because there better examples where go safety is a real benefit.
评论 #17949908 未加载
alexottover 6 years ago
Can’t leave comment in blog, but the first two listings are broken - it looks like there is no empty line after ``` in listing line, and before the same in listing two. So enumeration isn’t rendered correctly
raverbashingover 6 years ago
Good tips, unfortunately, the explanations are not very clear and it&#x27;s written in poor English, this gets very distracting.
评论 #17945887 未加载
评论 #17947711 未加载
tomohawkover 6 years ago
Regarding the defer statements in loops. It is better to just not ever do that.<p>Instead, move the defer outside of any loops, and have the defer check the state of the variable to decide action. For example, in the case of an open file, the loop should close the file and nil out the reference. In the defer, if the reference is not nil, it closes the file.
评论 #17945777 未加载
dis-sysover 6 years ago
for goroutine leaks, I have been using the leaktest package made by fortytw2 [1] in all my tests, pretty useful for my day-to-day dev work.<p>the article is pretty cool, especially when the author actually has a family name Check. :)<p>[1] <a href="https:&#x2F;&#x2F;github.com&#x2F;fortytw2&#x2F;leaktest" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;fortytw2&#x2F;leaktest</a>
axilmarover 6 years ago
I wouldn&#x27;t want to use a language that I cannot predict how memory allocation would work. I&#x27;d like to have every inch of performance in my disposal for the tasks I am interested in (video games, simulations, data mining).
nialv7over 6 years ago
Sounds more like design mistakes of Go.
ncmncmover 6 years ago
1.There is no such language as C&#x2F;C++.<p>2. There are no developers of C&#x2F;C++.<p>3. C and C++ are distinct languages.<p>4. Anybody advertising for a C&#x2F;C++ developer is insufficiently aware of software requirements to provide meaningful employment.<p>5. C++ programmers and C programmers have different responses to unfamiliar languages.<p>6. Some C programmers would like to be thought of as skilled &quot;C&#x2F;C++&quot; programmers. There are none. Thus, they are not.<p>7. Many C++ programmers are capable of altering C code. They, also, are not &quot;C&#x2F;C++&quot; programmers. When altering C code, they are programming in C, not C++.<p>8. C++ programmers and C programmers make different kinds of mistakes.<p>9. Anyone titling an article about &quot;C&#x2F;C++ programmers&quot; make is making a fundamental category error, and has self-identified as lacking insight.<p>10. Anyone titling an article about mistakes &quot;C&#x2F;C++ programmers make&quot; refers to an empty set of programmers.<p>11. People new to Go make mistakes. (One such mistake might be using Go at all; that is not decided.)<p>12. There will be some intersection between mistakes made by any two groups of programmers.<p>13. The intersection between mistakes made by Java programmers and Javascript programmers does not define a &quot;Java&#x2F;Javascript&quot; language, nor a set of &quot;Java&#x2F;Javascript programmers&quot;, despite any similarity of names or surface syntax between the two.
评论 #17947250 未加载
评论 #17949925 未加载
评论 #17946788 未加载