Loading Go Minifier...

How to Minify Go Code - Step by Step Guide

Step 1

Input Your Go Code

Start with any Go source file, a whole package file or just a few functions. There are three ways in:

•
Paste directly: Copy your Go code and paste it into the input editor
•
Upload a file: Click "Upload" to pick a .go or .txt file from your computer
•
Try the sample: Click "Sample" to load a commented example program

Example: Go Code with Comments and Whitespace

Here's typical well-commented Go code with generous formatting:

// Package main provides user management functionality
package main

// User represents a system user with authentication details
type User struct {
    // Name is the user's full name
    Name   string
    // Email for notifications
    Email  string
    // Active status flag
    Active bool
}

/* ValidateUser checks if user data is valid.
   Returns true if all fields are properly set. */
func ValidateUser(u User) bool {
    return u.Name != "" && u.Email != ""
}
Step 2

Let It Minify

Shortly after you stop typing, the minifier reads your code token by token and:

•
Removes comments: Line (//), block (/* */) and doc comments go, except build directives such as //go:build and //go:embed and the cgo preamble above import "C"
•
Removes whitespace: Indentation, blank lines and spaces around operators and brackets go; a space stays only where two words would otherwise merge
•
Adds the semicolons Go expects: Go ends a statement at a line break after a name, literal, closing bracket, ++, -- or return, so a semicolon is written in each of those places when lines are joined (the rule in the Go spec)
•
Leaves literals alone: Strings, raw strings, runes and numbers are copied character for character

Example: Minified Output

The code from Step 1 after minifying. It is one line, so the statements are separated by semicolons:

package main;type User struct{Name string;Email string;Active bool};func ValidateUser(u User)bool{return u.Name!=""&&u.Email!=""}

Result: 469 bytes down to 129 bytes, about 72% smaller. Most of the saving here comes from the comments.

Step 3

Copy or Download

When the output looks right, take it with you:

•
Copy: Puts the minified code on your clipboard
•
Download: Saves it as minified.go
•
Size badge: Shows the size before and after in bytes, and whether the file got smaller or larger

Example: Build Directives Survive

A file with a build constraint and an embedded file keeps those lines, because Go reads them when it builds:

//go:build linux

package main;import(_"embed";"fmt")
//go:embed hello.txt
var greeting string;func main(){fmt.Println(greeting)}

Want the opposite? The Go Formatter lays code back out in gofmt style.

Frequently Asked Questions

Will the minified Go code still compile?

If it compiled before, it should compile after. Go ends a statement wherever a line ends after a name, a literal, a closing bracket, ++, -- or return, so joining lines blindly breaks code. This minifier follows that rule and writes a semicolon at each of those line ends, and keeps a space only where two tokens would otherwise run together. It doesn't type-check your program, though, so code that didn't build before won't build after either.

Are strings, runes and numbers kept exactly?

Yes, byte for byte. Double-quoted strings keep their spacing and escapes, raw `backtick` strings keep their line breaks and anything that looks like a comment inside them, and runes such as '\'' are untouched. Numbers are copied as written, so 0x1F, 1_000, 14.50 and a long ID like 8901240544102066246 come out the same.

Which comments are kept?

Ordinary comments, including doc comments, are removed. Comments that change how the file is built stay on their own lines: //go:build and // +build constraints (with the blank line Go needs after them), //go:embed, //go:generate, //go:noinline and other //go: directives, //export, //line, and the cgo preamble, which is the C code in the comment right above import "C".

How much smaller will my file get?

It depends on how much of the file is comments and indentation. The sample on this page goes from 946 to 316 bytes, about 67% smaller, because it is heavily commented. Tightly written code shrinks less, since names are never shortened. The badge next to Minified Output shows the real size before and after, counted in UTF-8 bytes.

What happens if my code has a mistake?

You get a message with the line and column instead of output, for example an unclosed string, raw string or /* comment, a bracket that is never closed or doesn't match, or a character Go doesn't allow outside a string. Pasting JSON by accident is caught too. Errors that only the compiler finds, like an undefined variable, aren't checked here.

Should I ship minified Go code?

There is little reason to. Go compiles to a binary, and comments and whitespace don't change its size or speed. Minifying is handy for code golf, for squeezing a snippet into a size-limited field or message, or for seeing how much of a file is commentary. Keep the readable version in your repository.

Is my code sent anywhere?

No. The minifier runs in your browser, nothing you paste or upload goes to our server, and nothing is kept: reload the page and the editor is empty again.