b2KIT

Go Formatter (gofmt)

Format Go source code to canonical gofmt style with proper indentation and spacing.

Tested tool guide Tested browser tools Checked August 16, 2026

What Go Formatter (gofmt) does, with a checked example

Go source pasted into this formatter is rewritten to the canonical layout produced by gofmt: tabs for block indentation, conventional spaces around tokens, blank lines between major sections, and normalized alignment. It is intended for Go files whose syntax is already valid, and its result can be compared directly with code formatted by the standard Go toolchain. The common surprise is that gofmt is intentionally not configurable. There is no project-specific brace style, indentation width, or maximum line length to select. It also does not correct program logic or repair invalid syntax.

Worked example

A concrete input and expected output from the current implementation.

Input

package main
import "fmt"
func main() {
fmt.Println("hi")
}

Expected output

package main

import "fmt"

func main() {
	fmt.Println("hi")
}

The formatter separates the package clause, import declaration, and function declaration with blank lines. It indents the function call with a tab and adds a final newline, while leaving the function header, call, and string value unchanged.

How the result is produced

1

Fixed syntax-aware layout

gofmt applies one fixed layout to valid Go syntax. It normalizes block indentation, token spacing, declaration layout, composite literal layout, and the visual alignment of comments. The source structure determines the result, so repeated formatting is stable: once text is in gofmt form, formatting it again does not introduce another style change.

2

Import handling and scope

Existing import declarations are formatted, and imports within a parenthesized group are sorted. This is narrower than dependency management: gofmt does not inspect unresolved identifiers to add packages, and it is not a substitute for goimports. The result is formatted source only; the operation does not compile the package, execute tests, run vet checks, or establish that the program behaves correctly.

Good uses

  • Normalize a hand-edited .go file before committing so its whitespace and declaration layout match a repository check that requires gofmt output.
  • Clean a copied Go example whose block indentation and token spacing were damaged by a document, web page, or chat transcript.
  • Produce canonical formatting for a small source sample used in a code review, teaching exercise, documentation page, or formatting snapshot test.

Limits and checks

  • The input must be parseable Go source. A missing delimiter or incomplete declaration is a syntax problem, not something formatting can repair.
  • A clean result proves only that the text can be formatted. It does not prove the package builds, imports are used, types match, tests pass, or the code is safe.
  • Do not read an unchanged long line as a failure. gofmt has no configurable column limit and does not promise to wrap every expression to a preferred width.

Common questions

Will gofmt change what my program does?

For valid Go source, gofmt is intended to change presentation, not program logic. It can normalize whitespace, line breaks, alignment, and the order of existing import specifications. It does not rename identifiers, select different algorithms, or reorder executable statements. Review the diff when comments or generated files are sensitive to exact layout, but ordinary formatting is not a refactoring or optimization.

Can I choose spaces instead of tabs or set a line width?

No. Canonical gofmt style is deliberately fixed rather than controlled by a project style file. Indentation uses tabs, with spaces used where alignment requires them, and there is no maximum-line-length setting. If a repository requires a different visual style, this formatter cannot reproduce that style while also producing exact gofmt formatting.

References and verification

The example and behavioral notes were checked against the browser implementation. Standards and primary references below define the relevant format, formula, or platform behavior.

Related Tools