Go语言中常见的陷阱有哪些?

huangapple go评论95阅读模式
英文:

What are the common pitfalls in the Go language?

问题

我计划对Golang进行一些程序分析,就像pylint一样,试图从源代码中找出问题。所以我需要首先问的问题是:

Go语言中常见的陷阱有哪些?

我知道Python中有一些,例如将数组变量'[]'作为参数,以及可变对象与不可变对象之间的区别(在这里和那里可以找到更多信息)。

示例1:

>>> def spam(eggs=[]):
...     eggs.append("spam")
...     return eggs
...
>>> spam()
['spam']
>>> spam()
['spam', 'spam']
>>> spam()
['spam', 'spam', 'spam']
>>> spam()
['spam', 'spam', 'spam', 'spam']

示例2:

Map<Person, String> map = ...
Person p = new Person();
map.put(p, "Hey, there!");

p.setName("Daniel");
map.get(p);       // => null

所以我真正想知道的是Golang中类似的陷阱。我已经搜索了网络,但找不到相关信息。我还查看了一些git仓库的历史记录(例如docker.io),但没有找到典型的例子。你能否提供一些示例,例如内存泄漏、并发、意外结果和误解等方面的问题?

英文:

I am planning to do some program analysis on Golang, just like pylint, trying to find issues from source code. So the first question I need to ask is:

> What are the common pitfalls specialized in the Go language?

I know there are some ones in Python, e.g. array variables '[]' as parameters, and mutable vs immutable objects. (See more here and there).

Example 1:

&gt;&gt;&gt; def spam(eggs=[]):
...     eggs.append(&quot;spam&quot;)
...     return eggs
...
&gt;&gt;&gt; spam()
[&#39;spam&#39;]
&gt;&gt;&gt; spam()
[&#39;spam&#39;, &#39;spam&#39;]
&gt;&gt;&gt; spam()
[&#39;spam&#39;, &#39;spam&#39;, &#39;spam&#39;]
&gt;&gt;&gt; spam()
[&#39;spam&#39;, &#39;spam&#39;, &#39;spam&#39;, &#39;spam&#39;]

Example 2:

Map&lt;Person, String&gt; map = ...
Person p = new Person();
map.put(p, &quot;Hey, there!&quot;);

p.setName(&quot;Daniel&quot;);
map.get(p);       // =&gt; null

So what I really want to know is the similar ones in Golang. I have search the web but cannot find it. I also review some git repository history (e.g. docker.io) but cannot get typical ones. Could you kindly provide me some examples such as memory leaks, concurrency, unexpected results, and misunderstandings.

答案1

得分: 4

Golint 是用于 Go 代码的一个代码检查工具。

英文:

Golint is a linter for Go source code.

huangapple
  • 本文由 发表于 2014年1月23日 09:09:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/21297386.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定