Go Formatter
Format and beautify Go source code — tab indentation derived from brace/paren depth, Go's mandatory same-line brace placement, unambiguous operator/comma spacing, composite-literal colon spacing, and optional stdlib/third-party import sorting — while leaving raw `backtick` strings and comments byte-for-byte untouched.
Input
Formatting Options
Output
Guides
Paste messy Go and get back clean, gofmt-conventional source — instantly, in your browser. The Go Formatter re-indents with tabs, moves every opening brace onto the same line as the if/for/func/struct it belongs to, tidies spacing around operators and commas, and can sort and group your imports into standard-library and third-party blocks — all while leaving raw `backtick` strings and comments completely untouched.
Why tabs, and why the brace never moves
Two things about Go formatting aren't really style choices — they're the language's own convention. Indentation is always tabs, never spaces — this tool doesn't offer a spaces option, because gofmt doesn't either. And the opening brace stays on the same line as the keyword that owns it: Go performs automatic semicolon insertion at the end of a line, so a lone { on its own line can silently change what the code means, not just how it looks. This tool always merges a stray brace back onto the previous line.
How to use it
Paste your Go source, or upload a .go file. Toggle Sort & group imports if you want your import block reordered into a standard-library group and a third-party group, each sorted alphabetically. Copy the result, or download it as formatted.go. Formatting runs automatically as you type or change the option.
What it normalizes
- Indentation — tabs, from brace/paren nesting depth.
case/defaultdedent one level from theirswitchbody. - Brace placement — a lone
{line is always merged onto the previous line. - Operator spacing — unambiguous binary operators get single-space padding:
:=,==,!=,<=,>=,&&,||,<<,>>,&^,=, and the augmented-assignment forms. - Comma and semicolon spacing — no space before, one space after.
- Composite-literal colons —
User{Name:"x"}becomesUser{Name: "x"}, while slice/index colons stay tight (s[1:3]). This is a real disambiguation, not a guess: it tracks the type of the innermost open bracket across the file, and only spaces a colon when that bracket is{, never[. struct{,interface{, and parenless statement headers (if x > 0{,for i := range xs{) get a space before the brace, as do function signatures likefunc f() error{.- Import sorting (optional) — standard library and third-party groups, each sorted alphabetically, separated by a blank line.
- Blank lines — collapsed to at most one in a row, none right after
{or right before}, and exactly one inserted before a top-levelfunc/type/var/const.
What it deliberately leaves alone
- Unary vs. binary
+,-,*,&, and the channel operator<-(binary in a send, a tight unary prefix in a receive). Telling these apart needs real parsing, so their existing spacing survives unchanged. - Line wrapping — nothing is ever reflowed onto more or fewer lines than you gave it.
- Anything inside a string. Raw
`...`strings are preserved **byte-for-byte** — newlines, indentation, braces and colons included — since they're commonly used for SQL and templates. Struct tags (`json:"name"`) are likewise untouched internally. - Comments. Both
//and/* */comments, including multi-line block comments, are copied through verbatim.
Is it the same as gofmt?
No — it's a dependency-free text formatter that runs entirely in your browser, not a real Go parser. gofmt builds a full syntax tree; this tool works on tokenized text, which gets correct tabs, correct brace placement, and sane spacing without installing Go.
Is my code private?
Completely. All formatting happens locally in your browser — your source is never sent to a server, logged, or stored.
Use it from code
From 3 credits per callREST API
curl -X POST https://api.iotools.cloud/v1/tool/go-formatter \
-H "Authorization: Bearer YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"goInput": "package main\n\nimport (\n\"fmt\"\n\"os\"\n\"github.com/sp…",
"sortImports": "true"
}'Swap in your own key from your account. The tool's fields are the body — no wrapper.
Ask an AI agent
Use the IOTools `go-formatter` tool (Go Formatter) on this input:
YOUR_INPUT_HEREPaste this at any agent connected to the IOTools MCP server, then add your input.