Loading Go Beautifier...

How to Beautify Go Code That Lost Its Formatting

Go code rarely stays tidy once it leaves the editor. Paste a function into a ticket, a chat or a CMS and the tabs disappear, spaces pile up in odd places, and the struct fields no longer line up. This page puts the layout back the way gofmt would, without a Go toolchain on the machine you happen to be using.

Step 1

Paste the messy code

Paste the Go code you copied from a chat, an email, a blog post or a log into the left editor, or upload a .go file. Sample loads a small program with all of its indentation stripped out.

Example: code pasted out of a chat message

The indentation is a random mix of spaces, and the spacing around := and != has gone.

package main
import (
"encoding/json"
"fmt"
)
type Order struct{
  ID int64 `json:"id"`
      Zip string `json:"zip"` // keep the leading zero
  Price float64 `json:"price"`
}
func main(){
   o:=Order{ID:8901240544102066246,Zip:"00501",Price:14.50}
        b,err:=json.Marshal(o)
  if err!=nil{
      panic(err)
  }
fmt.Println(string(b))
}
Step 2

Let it re-indent

About half a second after you stop typing, the right panel shows the code with tab indentation, gofmt spacing around operators and after commas, aligned struct fields, tags and trailing comments, and a blank line between top-level declarations. If a bracket or string was cut off while copying, you get the line and column of the problem instead.

The same code after beautifying

This is exactly what gofmt prints for the same input. The struct tags and the comment line up, and the long ID, the zip code and 14.50 are untouched.

package main

import (
	"encoding/json"
	"fmt"
)

type Order struct {
	ID    int64   `json:"id"`
	Zip   string  `json:"zip"` // keep the leading zero
	Price float64 `json:"price"`
}

func main() {
	o := Order{ID: 8901240544102066246, Zip: "00501", Price: 14.50}
	b, err := json.Marshal(o)
	if err != nil {
		panic(err)
	}
	fmt.Println(string(b))
}
Step 3

Switch to Minify if you need one line

Minify does the opposite: comments and line breaks go, and semicolons are written where Go would insert them. Press Format to get the readable version back.

Step 4

Copy or download

Copy puts the result on your clipboard. Download saves it as formatted.go, or minified.go when Minify is on. If the code still looks wrong after beautifying, check it with the Go Validator; if you are tidying your own work rather than rescuing a paste, the Go Formatter page covers the gofmt rules in more detail.

Frequently Asked Questions

My Go code lost its indentation when I pasted it. Can this put it back?

Yes, as long as the line breaks survived. Chat apps, email clients and some CMS editors throw away leading tabs or turn them into a random number of spaces. The beautifier ignores whatever indentation is there, works out the nesting from the braces and parentheses, and writes one tab per level, the way gofmt does. Squashed spacing like o:=Order{ID:1} or if err!=nil{ is fixed at the same time.

Will it unpack Go code that is all on one line?

No. Go treats the end of a line as a semicolon, so this page never moves code onto a new line or joins two lines together; it only changes the spaces and tabs between tokens. Minified code like type User struct{Name string;Age int} comes back as type User struct{ Name string; Age int }, spaced out but still on one line, where gofmt on your own machine would put each field on its own line. If you have no Go toolchain at hand, add line breaks after the { and ; yourself and paste it again.

Is the result the same as running gofmt?

For code that still has its line breaks, usually yes. The Order example on this page comes out byte for byte the same as gofmt. A few things are left as you wrote them: one-line blocks such as if ok { return } are not spread over several lines, imports are not sorted, and multi-line /* */ comments keep their own indentation. If your team checks formatting in CI, run gofmt before committing.

Can beautifying change what my program does?

No. Identifiers, strings, raw `backquote` strings, runes and numbers are copied exactly, so 8901240544102066246, "00501" and 14.50 stay as typed. Before the result is shown, the page reads it again and checks that it has the same tokens, with semicolons in the same places, as your input.

What happens if the code I pasted is broken?

Copying from a web page often cuts off the last brace or a closing quote. Instead of guessing, the beautifier stops and tells you the line and column: a bracket that is never closed or closed by the wrong partner, a string that runs past the end of the line, an unclosed raw string or /* comment, a missing comma at the end of a line inside ( ) or [ ], or a character Go does not allow. Type errors and undefined names are not checked here; the Go Validator and go vet are better for that.

How is this page different from the Go Formatter?

Both run the same formatting code, so the output is identical. The Go Formatter is written for tidying code you are working on before a commit; this page is about rescuing code that arrived in a bad state from somewhere else. Use whichever you have bookmarked.

Does my code leave my browser, and how big can it be?

The code is beautified in your browser and is not sent to our server, and nothing is kept: reload the page and the editor is empty again. A file of a few thousand lines takes well under a second. Uploads are limited to 10 MB.