Empty lines inside a import-declaration

Viewed 182

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 Answers

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.

Related