Loading Go Formatter...

How to Format Go Code

gofmt is the reason almost every Go file looks the same. This page applies the same layout rules in your browser, which helps when you are reading a snippet from a pull request, an issue or a chat message and don't have a Go toolchain at hand. See the Go blog post on gofmt for why Go settled on one style.

Step 1

Paste or upload your Go code

Paste a whole .go file or just a function into the left editor, upload a .go file, or press Sample to load an unindented example.

Example: code that lost its indentation

package main
import "fmt"
type User struct {
ID int
Name string // shown in logs
Tags []string
}
func main() {
user:=User{
ID:42,
Name:"Gopher",
Tags: []string{"admin","ops"},
}
if len(user.Tags)>1{
fmt.Println(user.Name, user.ID*2+1)
}
}
Step 2

Read the formatted result

Half a second after you stop typing, the right panel shows the code laid out the gofmt way: tabs for indentation, aligned struct fields, key: value pairs and trailing comments, and gofmt spacing around operators. If the code has an unclosed bracket or string, you get the line and column instead.

The same code after formatting

Note the tabs, the aligned field types and values, and the spacing: user.ID*2+1 stays tight because it is one of several arguments, exactly as gofmt prints it.

package main

import "fmt"

type User struct {
	ID   int
	Name string // shown in logs
	Tags []string
}

func main() {
	user := User{
		ID:   42,
		Name: "Gopher",
		Tags: []string{"admin", "ops"},
	}
	if len(user.Tags) > 1 {
		fmt.Println(user.Name, user.ID*2+1)
	}
}
Step 3

Minify if you need it short

Press Minify to drop comments and line breaks (Go semicolons are added where Go would insert them). Press Format to switch back.

Step 4

Copy or download

Copy puts the result on your clipboard; Download saves it as formatted.go (or minified.go). If you want a second check before committing, run the file through the Go Validator, or turn Go data into JSON.

Frequently Asked Questions

Is the output the same as running gofmt?

For ordinary code, yes. It indents with tabs, lines up struct fields, key: value pairs, const and var groups and trailing comments, puts the spaces around operators where gofmt does (x := a + b, but a*b + c and f(i+1, j)), and adds the blank line gofmt wants between different kinds of top-level declarations. A few things are left as you wrote them: one-line blocks such as if ok { return } are not split over several lines, imports are not sorted, and multi-line /* */ comments keep their own indentation. Run gofmt before you commit if you need a byte-for-byte match.

Can formatting change what my program does?

No. Only the spaces, tabs and blank lines between tokens change. Every identifier, string, raw `backquote` string, rune and number (0x1F, 1_000, 8901240544102066246, 14.50) is copied exactly, and code is never joined onto or split off another line, because Go adds a semicolon at the end of lines. Before showing the result, the page reads it again and checks that it contains the same tokens, with semicolons in the same places, as your input.

Which mistakes does it point out?

Brackets that are never closed or closed by the wrong partner, strings in double quotes that run past the end of the line, unclosed raw strings and /* comments, a missing comma at the end of a line inside ( ) or [ ], and characters Go does not allow, such as #. Each message gives the line and column. It is a formatter, not a compiler: undefined names, type errors and unused imports are not checked, so go vet and go build still have the last word.

What does Minify do?

It removes comments, indentation and line breaks and writes a semicolon wherever Go would have inserted one, so the code stays valid on a single line. Build constraints (//go:build), other //go: directives and the comment above import "C" are kept on their own lines because they change how the code builds. Minified Go is handy for pasting into a chat or a test fixture; it is not meant for your repository.

Do I need a full file with a package line?

No. A single function, a type declaration or a few statements can be formatted on their own. Code with a package clause is formatted as a complete file, including the blank lines gofmt puts between the package line, imports, types and functions.

Is my code uploaded anywhere?

No. Formatting runs in your browser, the code is not sent to our server, and nothing is kept: when you reload the page the editor starts empty.

How large a file can it handle?

A 4,000-line file such as net/http/server.go is formatted in well under a second on a laptop. Uploads are limited to 10 MB.