ModernCalcs

Go Formatter

Indent
package main

import (
"fmt"
"strings"
)

type User struct {
  Name string
  Email string
  Age int
}

func (u User) Greet() string {
  return fmt.Sprintf("Hello, %s!", u.Name)
}

func filterUsers(users []User, minAge int) []User {
  result := []User{}
  for _, u := range users {
    if u.Age >= minAge {
      result = append(result, u)
    }
  }
  return result
}

func main() {
  users := []User{
    {Name: "Alice", Email: "alice@example.com", Age: 30},
    {Name: "Bob", Email: "bob@example.com", Age: 25},
  }
  adults := filterUsers(users, 28)
  for _, u := range adults {
    fmt.Println(strings.ToUpper(u.Greet()))
  }
}

Note: Official Go style (gofmt) uses tabs, not spaces. This formatter uses spaces for readability in the browser. For committed code, run gofmt or use go fmt ./...

Go Formatter: Readable Go Code Without the gofmt Toolchain

Go is famously opinionated about formatting — gofmt enforces a single style and is non-negotiable in Go projects. But before you have gofmt available, or when reviewing a snippet pasted out of context, this browser-based formatter gives you properly indented Go code instantly.

Formula
// Input: flat func process(items []string) { for _, s := range items { if len(s) > 0 { fmt.Println(s) } } } // Output: indented func process(items []string) { for _, s := range items { if len(s) > 0 { fmt.Println(s) } } }

Go's opening brace must be on the same line as the statement — the formatter preserves this requirement.

Go's Formatting Philosophy

Go takes an unusual approach: there is one correct formatting style, enforced by gofmt. This eliminates style debates in code review. Every Go project uses gofmt and CI pipelines check it. The formatter here does not implement all of gofmt's rules but produces readable indentation for inspection.

Struct and Interface Formatting

Go structs and interfaces use { } blocks. The formatter indents field declarations and method signatures correctly inside their blocks. Go's gofmt also aligns field names and types in struct definitions — this browser tool does not implement that alignment.

gofmt vs This Tool

gofmt uses hard tabs, not spaces. It also handles: operator spacing, struct field alignment, and import grouping. This formatter uses spaces and handles only indentation depth. Use this for quick inspection; use 'go fmt ./...' for committed code.

Practical Examples

Reviewing a Go Snippet from Documentation

Format a Go code sample from docs or a blog post for easier reading.

  • 1.Paste the Go code (may be flat or inconsistently indented)
  • 2.The formatter applies brace-depth indentation
  • 3.Read through functions, types, and goroutine patterns
  • 4.Copy and paste into your editor to continue working

What Gets Formatted

  • func, struct, interface, if, for, switch blocks
  • Opening { preserved on same line (Go requirement)
  • 2 or 4 space indentation toggle
  • Multiple blank lines collapsed

Good Use Cases

  • Reading Go snippets from blog posts or documentation
  • Formatting AI-generated Go code before review
  • Quick inspection before setting up the Go toolchain
  • Teaching Go code structure to beginners

Frequently Asked Questions

Does this match gofmt output?

Not exactly. gofmt uses tabs for indentation, aligns struct fields, and enforces specific spacing rules around operators. This formatter uses spaces (2 or 4) and only applies brace-depth indentation — useful for quick readability, not as a gofmt replacement.

Why does Go require the opening brace on the same line?

Go's automatic semicolon insertion rule adds a semicolon after certain tokens at end-of-line. If you write 'func main()' on one line and '{' on the next, Go inserts a semicolon after the ')', causing a syntax error. The opening brace must be on the same line.

What is gofmt and how do I run it?

gofmt is Go's official formatting tool, included in the Go toolchain. Run 'go fmt ./...' to format all Go files in a module, or 'gofmt -w file.go' for a single file. IDEs with Go support (VS Code + gopls, GoLand) run it on save automatically.

Does this handle Go generics syntax?

Go generics use [ ] for type parameters, not { }. The formatter only uses { } for indentation, so generic type parameters on the same line are treated as regular text and formatted correctly.