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 says Wat

152 pointsby beliuover 6 years ago

21 comments

sacado2over 6 years ago
Hmm, the first few WATs are pretty bad examples. Any developer, gopher or not, should know that, when you reassign the value of a function parameter, the variable when the function returns still has its original value. It&#x27;s pass-by-reference 101. Now, sure, slices and the way append work are a bit complicated to grasp at first, especially if you never used C, but this is not a WAT.<p>A lot of WATs are more bad practice than problems in the language, IMO (like WAT 8, modifying the return value twice in deferred functions, with the order being significant: who the hell writes such a code?)<p>A few ones, though, really are problematic. For instance I&#x27;ve been bitten more than once by variable shadowing, especially with error values. IMO, this is the weakest point of Go.
评论 #17932998 未加载
评论 #17933665 未加载
评论 #17937390 未加载
评论 #17935110 未加载
评论 #17937991 未加载
评论 #17939535 未加载
krylonover 6 years ago
&quot;The order of iteration for a Go map (a hashmap) is unspecified, by design.&quot;<p>I that surprising behavior? It is not only well-documented, but is in line with most other languages that offer a hash table &#x2F; map &#x2F; dictionary &#x2F; whatever in the base language or standard library.
评论 #17935522 未加载
评论 #17938691 未加载
评论 #17942062 未加载
评论 #17939657 未加载
blixtover 6 years ago
I was really expecting the slice WATs to carry on to talk about what I consider a real WAT: you have to perform robust checks on your input slice&#x27;s capacity and length if you use append in a function, because append makes its own choice whether the array of a slice is reused or copied.<p>Here&#x27;s a basic demonstration:<p><a href="https:&#x2F;&#x2F;play.golang.org&#x2F;p&#x2F;8zMmPwtjcxG" rel="nofollow">https:&#x2F;&#x2F;play.golang.org&#x2F;p&#x2F;8zMmPwtjcxG</a><p>In particular, note how this behavior can easily be forgotten when the semantics are hidden through variadic arguments that are passed an existing slice (instead of the automatically created one when you pass in actual variadic arguments).
评论 #17933198 未加载
schmichaelover 6 years ago
I would add WAT 2.5: <a href="https:&#x2F;&#x2F;play.golang.org&#x2F;p&#x2F;gwcHh4-QhHK" rel="nofollow">https:&#x2F;&#x2F;play.golang.org&#x2F;p&#x2F;gwcHh4-QhHK</a><p><pre><code> func main() { x := []int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9} fmt.Println(&quot;orig: &quot;, x) mutate(x) fmt.Println(&quot;append: &quot;, x) mutate(x[:4]) fmt.Println(&quot;sliced: &quot;, x) } </code></pre> Appending to the end of the slice creates a new slice and therefore the caller doesn&#x27;t see the mutation.<p>However appending to a slice-of-the-slice leaves capacity in mutate&#x27;s copy-of-the-slice to append without allocating a new backing array. Therefore mutate happy bumps the len of its slice copy and mutates the callers slice!<p>This bit me once in real production code when reusing a []byte array to avoid allocations. The bug was obviously my fault, but this behavior can be easy to inadvertently trigger if you&#x27;re trying to avoid allocations!<p><i>Edit: fixed code formatting. It&#x27;s 2018 YC, please please please implement at least a subset of markdown.</i>
评论 #17938249 未加载
Groxxover 6 years ago
Nils are among the biggest wats in Go in my experience. &quot;useful nils&quot; just compounds &quot;the billion dollar mistake&quot; into something even more nefarious and even harder to identify during code review.<p>Along similar lines: WAT 15. Under what circumstances do you expect a nil var return to become non-nil? <i>Did you even know that was possible?</i>
评论 #17937176 未加载
leshowover 6 years ago
&gt; Go avoids magic<p>What? Go is all about magic. All the std lib stuff that is able to take any type works by magic.<p>Those &quot;obscure rules&quot; (which I prefer to call being able to reason about code) look like they are going to get added in Go 2.<p>It&#x27;s odd how programmers think polymorphism is this obscure thing when it&#x27;s available in almost every typed language.
评论 #17937539 未加载
jonbodnerover 6 years ago
Hi, I&#x27;m the presenter AMA.<p>One general comment; not every question was intended as a WAT. Some were setups to introduce a WAT.
评论 #17939100 未加载
评论 #17935206 未加载
mikelwardover 6 years ago
I feel like WAT3 is the only real WAT.<p>Looking up a key in a map you didn&#x27;t &quot;make&quot; gives you the zero value. I think that&#x27;s consistent with much of Go. But if appending to a nil slice works, assigning to a key should never panic, even if you didn&#x27;t &quot;make&quot; it first.
caffeinewriterover 6 years ago
I feel like every language should have at least one &quot;wat&quot; style talk or article. Even for languages you adore, it&#x27;s important to understand the quirks, shortcomings, and issues in a context that might seem absurd to an outside developer.
spepsover 6 years ago
WAT 16 is the most shocking really. Reminds me of Python 2 where True and False can be overridden as well, it was fixed in Python 3.
评论 #17938900 未加载
评论 #17936186 未加载
评论 #17932964 未加载
评论 #17938879 未加载
评论 #17933328 未加载
stcredzeroover 6 years ago
<i>nil is weird in Go, and in most languages with an equivalent concept. It&#x27;s a &quot;hole in the type system&quot;</i><p>In Smalltalk, nil just contains the sole instance of the UndefinedObject class. It&#x27;s not a hole in the type system. Instead, it becomes a paradox in the inheritance system, because it&#x27;s used as a superclass.<p>Here&#x27;s a WAT. Smalltalk is actually strongly typed. It&#x27;s just that everything has the same type of Object. (The type system has no holes. However, it&#x27;s the size of a thimble!)
kbdover 6 years ago
I didn&#x27;t finish reading all the WATs, but if these are the best WATs about Go, it must be a very consistent language.<p>The first two, for example, aren&#x27;t WATs at all. The Go book is very clear that you need to use the result of `append` to get the modified slice. WAT 4, map traversal is unordered, is not at all a WAT. WAT 7, maps are reference types, is also not surprising at all. WAT 8 is also not a WAT, defers are defined to be processed in LIFO order, and the rest falls out of how named returns work. This was a waste of time :(
评论 #17938412 未加载
nemo1618over 6 years ago
You call that a WAT? I&#x27;ll show you a WAT.<p><a href="https:&#x2F;&#x2F;play.golang.org&#x2F;p&#x2F;ePRpDbaFaRl" rel="nofollow">https:&#x2F;&#x2F;play.golang.org&#x2F;p&#x2F;ePRpDbaFaRl</a>
评论 #17939632 未加载
songgaoover 6 years ago
This old blog post has a pretty good explanation about how Go slice works: <a href="https:&#x2F;&#x2F;blog.golang.org&#x2F;go-slices-usage-and-internals" rel="nofollow">https:&#x2F;&#x2F;blog.golang.org&#x2F;go-slices-usage-and-internals</a><p>Once you realize slices are just (pointer, length, capacity) structures, and the structure itself is copied by value, the first 2 WAT is pretty trivial.
评论 #17938477 未加载
评论 #17940457 未加载
ainar-gover 6 years ago
WAT 3 and WAT 10 were surprising for me.<p>WAT 3 becomes less surprising when you consider that a method on a pointer can be called and can return a valid result even if the pointer is nil.<p>WAT 10 just seems inconsistent. I said this before, and I&#x27;ll say it again: shadowing does more harm than good.<p>The rest are pretty trivial for anyone who worked with the language for over a year or read the documentation carefully.
Thorrezover 6 years ago
Now combine that with the extremely confusing net.IP and net.IP.To16() and you can get a sneaky bug that I&#x27;ve seen in practice.<p><a href="https:&#x2F;&#x2F;play.golang.org&#x2F;p&#x2F;SGgR4RSCPvM" rel="nofollow">https:&#x2F;&#x2F;play.golang.org&#x2F;p&#x2F;SGgR4RSCPvM</a>
jonbodnerover 6 years ago
The video for the talk is now available: <a href="https:&#x2F;&#x2F;youtu.be&#x2F;zPd0Cxzsslk" rel="nofollow">https:&#x2F;&#x2F;youtu.be&#x2F;zPd0Cxzsslk</a>
bradknowlesover 6 years ago
So, for those of us who are not GO-fers, what is a WAT?
评论 #17939709 未加载
IshKebabover 6 years ago
Except for the first two I don&#x27;t these are any where near as bad as the original Wat ones.<p>Still, nice list of small gotchas.
kc1116over 6 years ago
Feels like a lot of work went into coming up with these WATS
cliffordthedogover 6 years ago
Number 4 isn&#x27;t really a wat, it&#x27;s just a design choice. other languages offer maps that do retain order.