Go Formatter

Paste Go code and get back a reindented version based on brace nesting depth. This is a lightweight structural reindenter, not a full formatter like gofmt, which requires the Go toolchain. A free online tool from Staaarter, right in your browser.

Runs locallyUpdated 2026-07-24

Overview

Introduction

Go code pasted from documentation or a non-gofmt-formatted source often has indentation that doesn't match its brace structure.

This tool reindents each line based on `{ }` nesting depth, entirely in your browser, without requiring the Go toolchain.

What Is Go Formatter?

A lightweight reindenter that tracks brace, parenthesis, and bracket depth line by line and re-indents accordingly, using spaces rather than gofmt's tabs.

It's a whitespace-only pass: tokens and logic are never touched, only leading indentation.

How Go Formatter Works

Each line is scanned for `{}/()/[]` characters to compute a running nesting depth.

Lines that start with a closing brace are dedented one level before printing, matching their opening brace.

When To Use Go Formatter

Use it when a Go snippet's indentation has drifted after manual edits.

It's a quick visual check that braces are balanced.

Often used alongside C Formatter, Java Formatter and Rust Formatter.

Features

Advantages

  • Runs entirely client-side with no Go toolchain required.
  • Fixes indentation drift instantly for typical brace-delimited code.
  • Handles arbitrarily deep nesting without any configuration needed.

Limitations

  • Uses spaces, not gofmt's canonical tabs.
  • Not a real parser: brace characters inside string literals or comments still affect depth. Always run `gofmt` before committing Go code.

Examples

Drifted indentation

Input

func greet(name string) string {
return "Hello, " + name
}

Output

func greet(name string) string {
    return "Hello, " + name
}

Content inside the function body indents one level deeper, matching the opening brace.

Best Practices & Notes

Best Practices

  • Use this only for a quick visual cleanup; always run the real `gofmt` before committing Go code, since Go tooling and the community expect its exact output.
  • Don't check in the tabs-vs-spaces difference as a real diff; treat this tool's output as scratch space, not the final commit.

Developer Notes

Implemented as a single-pass line scanner that tracks a running brace/paren/bracket balance; it does not tokenize strings or comments, and does not replicate gofmt's tab-based indentation.

Go Formatter Use Cases

  • Quickly cleaning up indentation in a pasted Go snippet before running gofmt
  • Spotting an unbalanced brace by eye

Common Mistakes

  • Committing this tool's output directly instead of running it through the real `gofmt`, which Go projects expect verbatim.
  • Assuming brace characters inside string literals or comments are ignored, they aren't; the scanner counts every `{`, `}`, `(`, `)`, `[`, or `]` regardless of context.

Tips

  • Always pipe the final code through `gofmt` locally; this tool is a stopgap for quick browser-based cleanup, not a replacement.
  • Diff the reindented output against your original before running gofmt, so you can see exactly which lines shifted.

References

Frequently Asked Questions