导入声明中的空行

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

Empty lines inside a import-declaration

问题

我正在学习Go语言,按照教程进行学习。我不想盲目地复制示例代码,而是想理解其中的原理。在教程中,import语句被写成了这样:

import (
    "fmt"

    "example.com/greetings"
)

一开始,我对两个字符串之间的空行感到惊讶,以为这只是遵循某种风格指南,所以在编写我的程序时,我简单地写成了这样:

import (
    "fmt"
    "example.com/greetings"
)

令我惊讶的是,当我保存代码时(当时我在使用Visual Studio Code进行编辑),编辑器会重新插入空行,所以我认为这一定有更深层次的原因。

我没有看到这个空行对我的程序产生任何影响,因为当我后来使用一个不那么自作聪明的编辑器(nano)删除了这个空行时,我的程序似乎与包含空行时运行的结果相同...但也许我漏掉了什么。所以,我的问题是,这个空行有没有更深层次的含义,我还没有理解到?

英文:

I'm learning the Go language by following a tutorial. Not wanting to just blindly copying the examples, but understanding what's going on, I came accross the following puzzling case:

In the tutorial the import statement was written as:

import (
    "fmt"

    "example.com/greetings"
)

I first was surprised about the empty line between the two strings and thought that this is just following some style guide, and when creating my program, I wrote it simply as

import (
    "fmt"

    "example.com/greetings"
)

To my astonishment, when saving the code (I was using Visual Studio Code for editing at that time), the editor re-inserted the empty line, so I reckon that there must be a deeper reason for it.

I can't see any effect on my program, because when I later removed the line with a less presumptuous editor (nano), my program seemed to run the same as with empty line included ... but maybe I just missed something. So, my question, does this empty line have a deeper meaning, which I just don't grasp yet?

答案1

得分: 4

import组中的空行没有任何意义。

一些编辑器(包括VSCode)会先放置标准库的导入,然后添加一个空行,然后是其他的第三方导入。而且这两个组都按字母顺序排序。

再次强调,这样做没有其他意义,只是为了更容易阅读。如果每个人都按照这种方式格式化导入,那么在版本控制系统中就不会因为不同的导入排序/组织而产生“无意义”的提交。

英文:

There is no significance to the empty lines in the import group.

Some editors (including VSCode) put standard library imports first, then add an empty line, and then other (3rd party imports). Also the 2 groups are sorted alphabetically.

Again, there is no significance to this other than being easier to read. It also comes handy if everyone formats imports like this, so there are no "meaningless" commits in a version system due to different import sorting / organizing.

huangapple
  • 本文由 发表于 2021年6月9日 22:09:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/67905844.html
匿名

发表评论

匿名网友

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

确定