Go fmt: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
 
(3 intermediate revisions by the same user not shown)
Line 8: Line 8:
=Overview=
=Overview=


The <code>fmt</code> ("format") command formats source code files, by applying a predetermined layout to Go source code.
<code>gofmt</code> is a program that operates at source file level.


<code>go fmt</code> delegates to <code>gofmt</code>, and it is equivalent to:
<code>go fmt</code> operates at package level rather than source file level. <code>go fmt</code> delegates to <code>gofmt</code>, and it is equivalent to:


<syntaxhighlight lang='bash'>
<syntaxhighlight lang='bash'>
Line 16: Line 16:
</syntaxhighlight>
</syntaxhighlight>


One counterintuitive fact is that it uses the tab character for indentation. This is uncommon.
The tool formats source code files, by applying a predetermined layout, which includes a standard style of indentation and vertical alignment, and retains and if necessary reformats comments. For example, it lines up the fields of a structure and the associated comments. One counterintuitive fact is that it uses the tab character for indentation. This is uncommon.


Rob Pike: "[https://www.youtube.com/watch?v=PAAkCSZUG1c&t=523s gofmt style is no one's favorite, yet gofmt is everyone's favorite]".
Rob Pike: "[https://www.youtube.com/watch?v=PAAkCSZUG1c&t=523s gofmt style is no one's favorite, yet gofmt is everyone's favorite]".

Latest revision as of 23:25, 2 July 2024

External

Internal

Overview

gofmt is a program that operates at source file level.

go fmt operates at package level rather than source file level. go fmt delegates to gofmt, and it is equivalent to:

gofmt -l -w <file>

The tool formats source code files, by applying a predetermined layout, which includes a standard style of indentation and vertical alignment, and retains and if necessary reformats comments. For example, it lines up the fields of a structure and the associated comments. One counterintuitive fact is that it uses the tab character for indentation. This is uncommon.

Rob Pike: "gofmt style is no one's favorite, yet gofmt is everyone's favorite".