Since I started programming, I've always been taught to leave a trailing blank line at the end of my files, the reason usually being something relating to how it makes concatenated files easier to read when using cat.

While I can't find an example right now, GitHub indicates missing blank lines at the end of a file using a red symbol, or at least, used to - so clearly it's frowned upon by a considerable chunk of the community.

Working with Go lately, I noticed that gofmt doesn't like blank lines at the end of a file, and my Vim plugin removes them automatically.

Why are blank lines at the end of a file discouraged rather than enforced in Go?

  • No idea what you mean. I've got newlines at the end of all my files, and have done for ever. What you do not get is blank lines at the end of files. Is that what you meant? Feb 9 '16 at 6:15
  • @MichaelHampton that's indeed what I mean.
    – Jules
    Feb 9 '16 at 13:35
  • OK, please remember to edit your question! Feb 9 '16 at 14:54
  • What OS are you on, and what is your git setting for git config --get core.autocrlf
    – Pete
    Feb 9 '16 at 15:10

I had this same problem when I first switched to vim. Apparently it treats the newline character as the end of the line, unlike most other editors which treat it as a separator between lines.

If your save a file in vim without the extra black line and open it in another editor, you'll see the extra line. If you save a file without the extra line in another editor, and open it in vim, then it just has to assume the last line end with a newline. If you save the file it will add the line.

  • "unlike most other editors" - I've generally understood this to be a difference in conventions between Windows and *nix, not just across editors.
    – Izkata
    Apr 3 '16 at 7:36

It is not about GO but about POSIX standard that there should be a newline character at the end of every line.

POSIX: 3.206 Line A sequence of zero or more non-newline characters plus a terminating newline character.

So it is not coming from go but your editor itself that respect this standard. GNU Emacs, Vim ... they normally all respect that standard.

Go respect the standard, but, it create also its own and is able to run without that blank line Reading the code from go itself written by the authors of go, they use blank line at the end of their source code. Reading also in the Effective Go their is no information about blank line too. Nor in the golang.org/src/cmd/gofmt/gofmt.go source code itself There is nothing about this in all GOlang.org


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.