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.

Golang landmines

141 pointsby kornishover 8 years ago

14 comments

oppositelockover 8 years ago
I&#x27;ve been doing quite a bit of Go programming for a while now, and I&#x27;ve internalized all these behaviors, but this one gets me every time:<p><pre><code> var err error if true { foo, err := someFunction() &#x2F;&#x2F; err gets shadowed } </code></pre> It&#x27;s particularly annoying because if both foo and err are defined in the outer scope, you get a compiler error that no new variables are on the left, so you tend to forget that removing one shadows the other.
评论 #12526086 未加载
评论 #12525498 未加载
评论 #12526303 未加载
评论 #12525177 未加载
Animatsover 8 years ago
Languages probably shouldn&#x27;t allow shadowing, certainly not within a single module. Think of the maintenance programmers.<p>&quot;Defer&quot; was probably a bad idea. It&#x27;s a run-time construct - you&#x27;re adding to a to-do list run at the end of the function. It&#x27;s not scope oriented. Something like Python&#x27;s &quot;with&quot; clause would have been both simpler and more useful. &quot;with&quot; calls &quot;_enter&quot; on an object when you enter a scope, and &quot;_exit&quot; when you leave, no matter how you leave. All the file-like and lock-like objects support &quot;with&quot;. Python&#x27;s &quot;with&quot; even supports exceptions properly - if you&#x27;re handling an exception when &quot;_exit&quot; is called, the &quot;_exit&quot; function gets told about the exception, so it can clean up and then re-raise the exception. If an &quot;_exit&quot; itself needs to raise an exception, that works, too.<p>&quot;Defer&quot; is blind. With &quot;defer&quot;, nobody ever finds out that the close failed to write the end of the file.
评论 #12524635 未加载
评论 #12524100 未加载
评论 #12524505 未加载
评论 #12524208 未加载
评论 #12525478 未加载
retroencabulatoover 8 years ago
&gt; Everyone expects these values to be scoped inside the loop, but go reuses the same memory location for every iteration.<p>I don&#x27;t see this as surprising as a C++ user. Who is &#x27;everyone&#x27; here?
评论 #12524376 未加载
评论 #12524445 未加载
评论 #12524667 未加载
评论 #12523569 未加载
评论 #12523958 未加载
评论 #12523510 未加载
评论 #12523721 未加载
评论 #12523483 未加载
perfmodeover 8 years ago
It should be stated that entire teams build large systems with Go on a daily basis and don&#x27;t step on these.<p>The community of professional Go programmers has arrived at practices that avoid such issues.<p>However, I wonder how many new programmers fall off early because of these corner cases.
评论 #12523921 未加载
评论 #12524112 未加载
kowdermeisterover 8 years ago
Maybe it&#x27;s just me, but in these cases when I encounter some weird behavior in a language I assume that I don&#x27;t know enough of the language. It&#x27;s actually not the designers fault that they are there, but my knowledge that requires more info.
dpc_pwover 8 years ago
Golang is truely modern C. Popular, &quot;simple&quot; (in the twisted sense) and riddled with landmines. :)
评论 #12523949 未加载
评论 #12525949 未加载
tptacekover 8 years ago
By &quot;never do what GetACat()&quot; does, I think the author means &quot;always return structs, not interfaces&quot;.
评论 #12523325 未加载
评论 #12523227 未加载
zzzcpanover 8 years ago
For me channels and append() are the biggest language level landmines. Not so big are nils, :=, lack of panic when you go beyond last element of the slice, i.e. ([]int{0,1})[2:].<p>Libraries are a lot worse though, they are minefields.
评论 #12524787 未加载
sheeshkebabover 8 years ago
I&#x27;ve run into for loop go routine one just the other day... reminded me of groovy and it&#x27;s closure scopes too.
评论 #12523771 未加载
coldcodeover 8 years ago
As a Swift programmer Go programming seems riddled with a need to be perfect. I&#x27;d rather the language help me not make errors rather than assume I am perfectly aware of all the gotchas. Swift isn&#x27;t perfect but it does make things less likely to confound you later.
mmargerumover 8 years ago
I don&#x27;t know if I&#x27;d call this a land mine, but I wish it didn&#x27;t work because it confuses beginners.<p>You can type<p>for i:= range myvalues<p>In this case i is the index of the value. I&#x27;d rather they didn&#x27;t allow this and instead you would just type<p>for i,_ := range myvalues
bltover 8 years ago
Seems that the explicit lambda capture in C++ is a good way to avoid the first issue. Implicit capture of closure variables is kinda scary when you think about it.
oconnor663over 8 years ago
I would add taking a value receiver when you meant to take a pointer receiver.
karma_vaccum123over 8 years ago
If only one could go back in time and fix the weird overloading of nil for pointers and interfaces...it is the one breaking change to the Go1 language guarantee that seems worth it.
评论 #12523676 未加载