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.

Common Gotchas in Go

229 pointsby mreover 7 years ago

18 comments

dilapover 7 years ago
I think only #1 really counts as a gotchya. #3 is literally just &quot;spend at least 2 seconds knowing what a slice is&quot; (which you should do! there&#x27;s only like 4 data structures in Go built in, so get to know &#x27;em all). #2 is similar.<p>My favorite gotchya is that assigning a nil pointer to an interface will not give you a nil interface.<p>Once you know what&#x27;s going on, it totally makes sense, but usually you don&#x27;t start out with that deep an understanding of how interfaces work, and it violates natural-looking assumptions about algebraic equality, which makes for very easy counter-intuitive behavior.<p>Example:<p><pre><code> package main import &quot;fmt&quot; func main() { var x *ErrorX = nil var err error = x if err == nil { fmt.Println(&quot;nope, this won&#x27;t happen &quot;) } else { fmt.Println(&quot;here&#x27;s my non-nil error:&quot;, x) } } type ErrorX struct{} func (x *ErrorX) Error() string { return &quot;hi i&#x27;m ErrorX&quot; } </code></pre> <a href="https:&#x2F;&#x2F;play.golang.org&#x2F;p&#x2F;BB1b2_HDb6I" rel="nofollow">https:&#x2F;&#x2F;play.golang.org&#x2F;p&#x2F;BB1b2_HDb6I</a>
评论 #16050338 未加载
评论 #16050043 未加载
评论 #16050772 未加载
评论 #16049442 未加载
评论 #16055071 未加载
millstoneover 7 years ago
How do Go programmers deal with the fact that it is hard to reason about whether slices alias?<p>Go lacks const, so there&#x27;s no way to prevent your caller from modifying the slice you return, or the callee from modifying the slice you pass it. Also the result of append() may or may not alias its parameter, depending on the allocator&#x27;s mood that day.<p>Do Go APIs just defensively copy, or pepper APIs with &quot;do not modify&quot; warnings, or rely on runtime tests? Given the fact that append() may mask aliasing bugs, is there a way to make append() alias maximally?
评论 #16053035 未加载
评论 #16050569 未加载
评论 #16050502 未加载
yeukhonover 7 years ago
On #3 “slicing”, on a related note for Python, remember after a new list is created from slicing, each element&#x2F;item in the new list is the exact reference to the corresponding element&#x2F;item in the original list. This is espeically problematic when you try to do things on a mutuable object in either list, but expect the other list remains completely shield from change. You have to do deepcopy (but for immutable object deepcopy has no effect).<p>Makes sense and probably is a given for most programming languages.
JepZover 7 years ago
My newest two gotchas:<p>1. Don&#x27;t use private attributes for struct types if you ever want to serialize&#x2F;encode them (exception: Mutex and Channel Attributes). Just had to refactor a whole lot of code just, because I decided I wanted to save the struct to disc. Btw. the type itself can and probably should be private.<p>2. Don&#x27;t use Pointers in map key structs. As nesting maps is a pain the simple solution is to use structs as keys. But if you do so please remember not to use any pointer fields within those structs. Again when you encode and decode maps with pointers within the key structs those pointers will bite you.
评论 #16050483 未加载
评论 #16051791 未加载
评论 #16050981 未加载
maddybooover 7 years ago
Tl;dr: Understand the difference between values and pointers.
评论 #16052676 未加载
crypticlizardover 7 years ago
I liked how this was written. The way this was written sort of reminds me of go. It&#x27;s as simple as approriate, and written in a way to be memorable. Nice UX trick deadbeef!
nordsieckover 7 years ago
Syntax critique: <a href="https:&#x2F;&#x2F;play.golang.org&#x2F;p&#x2F;CjIDCu5ojru" rel="nofollow">https:&#x2F;&#x2F;play.golang.org&#x2F;p&#x2F;CjIDCu5ojru</a>
评论 #16049421 未加载
tapirlover 7 years ago
For #2, the &quot;if len(a) == 0&quot; clause is totally non-sense.<p>I hope Go supports &quot;append(nil, data[:2]...)&quot;, which will make code much cleaner.
评论 #16050982 未加载
drejover 7 years ago
The last gotcha sort of leads to a different gotcha. That copy statement will only work as expected if the destination slice is large enough - thus always initialise it with the length of the (sub)slice you want to copy over. You won&#x27;t get any errors if it&#x27;s smaller, but you will surely not get what you desired.
评论 #16050838 未加载
mappuover 7 years ago
My Go gotcha: x = append(y, ...) is usually unsafe for x != y.<p>I want to find&#x2F;build a linter that catches such cases...
评论 #16049928 未加载
评论 #16050822 未加载
nazkaover 7 years ago
There was a good post about many gotchas that was on HN a little while ago. Just to share :<p><a href="http:&#x2F;&#x2F;devs.cloudimmunity.com&#x2F;gotchas-and-common-mistakes-in-go-golang&#x2F;" rel="nofollow">http:&#x2F;&#x2F;devs.cloudimmunity.com&#x2F;gotchas-and-common-mistakes-in...</a>
jijjiover 7 years ago
it also takes some figuring that the first character of anything needs to be uppercase if 1) you try to a function outside of the main source file, and 2) use an element from a struct in a template, i.e. {{.element}} does not work...
ernsheongover 7 years ago
The first one could be mitigated by providing an array of article pointers ([]*Article). That way, article.legs = 99 would work.<p>Else, the way mentioned in the article, or (&amp;article).legs = 99 (ugly) would work. Take your pick.
评论 #16049573 未加载
innagadadavidaover 7 years ago
Looks like a newbie article. The gotcha that got had me a few times is closure within a loop:<p>for _, ele := range eles { go func() { &#x2F;&#x2F; use ele } }<p>Since ele is a loop variable it is not captured as you might think it is. You need to copy it before the closure.
评论 #16049523 未加载
tonethemanover 7 years ago
Yeah the range thing got me too.<p>At some point I tend to just ignore the second part of the range and only use the index...<p>and then after that I just switch back to a damn for loop like god intended.
dingo_batover 7 years ago
Didn&#x27;t read the article yet but congrats on the awesome domain name!
rplntover 7 years ago
I seriously need a browser extension to not render &quot;characters&quot; in color. How the hell did this happen anyway?
moocowtruckover 7 years ago
I look at stuff like this and wonder why I&#x27;m reading it because I&#x27;ll never use this language.. but i&#x27;m glad it&#x27;s here for people who do!