Go Formatter

Format Go with gofmt.

Runs locallyYour input is processed in this tab. The editor blocks outgoing network requests.
Go input

The Go Formatter runs gofmt, the formatter that ships with the Go toolchain, compiled to WebAssembly. It produces the one canonical layout every Go project uses: tabs for indentation, aligned struct fields and comments, and sorted imports. It is for Go developers checking a snippet without a local toolchain. Everything runs in your browser — nothing is uploaded.

How to format Go code with gofmt

  1. Paste your code, open a .go file with ⌘/Ctrl O, or load an example. A complete file with a package clause works, and so do declarations or statements on their own.
  2. There is nothing to configure: gofmt has one style, and formatting runs as soon as the input changes. If the code does not parse, the error panel shows gofmt's message.
  3. Copy the result with ⌥/Alt C or download it with ⌘/Ctrl S.

Examples

Struct and main

Blank lines are inserted between the package clause, imports and declarations; struct fields are aligned and blocks are indented with tabs.

Go
package main
import "fmt"
type User struct{Name string
Age int}
func main(){u:=User{Name:"Asha",Age:31}
if u.Age>18{fmt.Println(u.Name,"is an adult")}}

Result:

Go
package main

import "fmt"

type User struct {
	Name string
	Age  int
}

func main() {
	u := User{Name: "Asha", Age: 31}
	if u.Age > 18 {
		fmt.Println(u.Name, "is an adult")
	}
}

HTTP handler with an import list

A semicolon-separated import group is expanded one path per line and sorted, so encoding/json moves above net/http.

Go
package main
import ("net/http";"encoding/json")
func handler(w http.ResponseWriter,r *http.Request){w.Header().Set("Content-Type","application/json")
json.NewEncoder(w).Encode(map[string]string{"ok":"true"})}

Result:

Go
package main

import (
	"encoding/json"
	"net/http"
)

func handler(w http.ResponseWriter, r *http.Request) {
	w.Header().Set("Content-Type", "application/json")
	json.NewEncoder(w).Encode(map[string]string{"ok": "true"})
}

Struct tags and trailing comments

gofmt aligns field names, types, struct tags and trailing comments into columns.

Go
package config
type Config struct{
Host string `json:"host"`
Port int `json:"port"`
TimeoutSeconds int `json:"timeout_seconds"` // request timeout
Debug bool
}
var defaults=Config{Host:"localhost",Port:8080,TimeoutSeconds:30}

Result:

Go
package config

type Config struct {
	Host           string `json:"host"`
	Port           int    `json:"port"`
	TimeoutSeconds int    `json:"timeout_seconds"` // request timeout
	Debug          bool
}

var defaults = Config{Host: "localhost", Port: 8080, TimeoutSeconds: 30}

Options explained

The Go Formatter has no settings, and that is deliberate. gofmt was designed so that all Go code looks the same: indentation is always a tab, alignment is always spaces, and there is no line length to tune. The Go project's view is that arguments about formatting cost more than any particular choice, so the formatter offers nothing to argue about. Code formatted here is identical to what gofmt or go fmt produces on your machine, which is also what editors run on save and what CI checks with gofmt -l.

Troubleshooting gofmt errors

gofmt must parse the code before it can format it. Each error gives the line and column where parsing stopped, for example "(line 3, column 1)", and F8 moves the cursor there.

Unexpected semicolon or newline before {

The opening brace of a function, if, for or switch is on its own line. Go inserts a semicolon at the end of the previous line, so the brace must stay on the same line as the statement it opens.

Expected '}', found 'EOF'

A block is not closed. The position points at the end of the input; count braces upwards from there to find the block that is still open.

Illegal character U+003F '?'

Go has no ternary operator, so a > 0 ? 1 : 0 is not valid. Rewrite it as an if statement. Curly quotes pasted from a word processor or chat app produce a similar message, "Curly quotation mark '“' (use neutral '"')", which tells you to use a straight " instead.

Expected ';', found …

Two tokens follow each other where Go expects a statement to end, often because of a typo in a keyword (packag main) or a missing operator. The column points at the second token.

My unused import was not removed

gofmt only formats; it never adds or removes imports. goimports does that, and it is not part of this tool. The compiler will still report the unused import.

FAQ

Is this the real gofmt?

Yes. It is the gofmt formatting code from the Go distribution compiled to WebAssembly, so the output matches gofmt and go fmt for the same input. It runs without the -s simplify flag, so composite literals such as []Point{Point{1, 2}} keep their explicit types, exactly as plain gofmt leaves them.

Why does gofmt use tabs?

Tabs for indentation let each developer choose how wide an indent looks in their editor, while spaces for alignment keep columns lined up regardless of that choice. Go settled this once in gofmt, so every Go file uses the same convention. When you copy the output into a web page or document, set the tab width there if the indentation looks too wide.

Do I need a complete file with a package clause?

No. A full file is formatted as a file, but you can also paste a single function, a type declaration or a few statements. gofmt detects that the input is a fragment and formats it on its own, which is convenient for code taken from documentation, issues or chat messages.

Why did gofmt keep my function on one line?

gofmt respects some of the line breaks you write. A short function or block written entirely on one line, such as func main() { x := 1; y := 2 }, can stay on one line, while a body that already spans several lines is always expanded. Put a line break after the opening brace to get the multi-line form.

Is my code uploaded?

No. gofmt runs as WebAssembly inside your browser, and the editor sits in an isolated frame that blocks network requests, so nothing is uploaded or stored on a server. Code is only kept on your device if you turn on "Remember my last input", which is off by default.

Does gofmt enforce a maximum line length?

No. gofmt never wraps long lines, and there is no line-length setting to change. Long function signatures or calls stay as written; if you want them broken, add the line breaks yourself after a comma or opening parenthesis, and gofmt will indent the continuation lines consistently.

Other formatters

For the other languages in a Go service's repository, the Python Formatter handles scripts, and the C Formatter covers cgo sources. The complete list is on the code tools page.

Browse all Code Formatters