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.

Go is Not Python

25 pointsby blacksmythealmost 8 years ago

10 comments

optimusclimbalmost 8 years ago
I'm not sure what the title has to do with the article at all. It's just pointing out a language gotcha/subtlety. The title might as well have been "Go is Not Javascript", "Go is not QBasic", etc.
评论 #14615857 未加载
评论 #14615840 未加载
评论 #14615858 未加载
betterwaytodoitalmost 8 years ago
Here&#x27;s a more idiomatic translation of the Python code to Go, and there&#x27;s no surprise to how the code behaves:<p><a href="https:&#x2F;&#x2F;play.golang.org&#x2F;p&#x2F;rpqvVDR9v8" rel="nofollow">https:&#x2F;&#x2F;play.golang.org&#x2F;p&#x2F;rpqvVDR9v8</a>
评论 #14615885 未加载
评论 #14615863 未加载
jimsmartalmost 8 years ago
<p><pre><code> fredPtr = nil &#x2F;&#x2F; This line is superfluous. </code></pre> In Go, declarations always initialise variables to their zero value, which for all pointers is nil.<p>(Perhaps worthy of a follow up article: &quot;Go is Not C&#x2F;C++&quot;? ;)
majewskyalmost 8 years ago
Alternative options of solving this issue include `break`ing when the right option is found (so the loop variable doesn&#x27;t get overwritten again) or moving everything into a function:<p><pre><code> func findByName(students []*Student, name string) *Student { for _, s := range students { if s.name == name { return s } } return nil } </code></pre> Both options have the added advantage of stopping iteration as soon as the result is found.
grasleyaalmost 8 years ago
I think you can put this more generally as &quot;syntax is not semantics.&quot; Go takes some syntactic cues from python (but who doesn&#x27;t these days?) but its semantics can be pretty different. I think it&#x27;s pretty common for new programmers especially to get caught up in the syntax of languages without giving enough attention to their semantics, which causes the sort of problems the author highlights here.
评论 #14616606 未加载
abhinaialmost 8 years ago
I love Python. But honestly speaking, the point raised in this article is fairly trivial. I would not, not learn Go just because there are small differences in the way reference &#x2F; pointers work. I do not see this as an inherent shortcoming of Go.<p>Both languages have their strengths. When you pick a language for an application, it is your responsibility to use it properly. Otherwise you will make mistakes and it won&#x27;t be the fault of the language.
评论 #14615849 未加载
评论 #14615837 未加载
questhrowawayalmost 8 years ago
I don&#x27;t know Python or Go, but both examples seem _wrong_.<p>Python: How is `Fred` available to be printed in the Python example, is not it out of scope?<p>Go: Why use `var fredPtr *Student`? What&#x27;s the point? Can not use `var fredPtr Student` and the set `fredPtr = student` in the loop?
sabujpalmost 8 years ago
been a while since i&#x27;ve done pointer stuff, isn&#x27;t that how it works in c? go seems de-evolved to me.
评论 #14615920 未加载
评论 #14615908 未加载
urdaalmost 8 years ago
This has nothing to do with Python. It&#x27;s just a &quot;gotcha&quot; of Go.
评论 #14615833 未加载
singularity2001almost 8 years ago
for a no-goer: will this copy by value? ``` var fred Student for _, student := range students { if student.name == &quot;fred&quot; { fred = student } } ```
评论 #14617797 未加载