英文:
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:
>>> def spam(eggs=[]):
... eggs.append("spam")
... return eggs
...
>>> spam()
['spam']
>>> spam()
['spam', 'spam']
>>> spam()
['spam', 'spam', 'spam']
>>> spam()
['spam', 'spam', 'spam', 'spam']
Example 2:
Map<Person, String> map = ...
Person p = new Person();
map.put(p, "Hey, there!");
p.setName("Daniel");
map.get(p); // => 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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论