Go Obfuscator - Obfuscate Go Code to Protect Source
Scope-aware renaming of local variables, parameters, named returns, and receivers, string encoding with a matching runtime decoder, and comment removal — with import-path, const, and struct-tag-safe string handling, plus ASI-correct minification. Every transform runs locally in your browser.
Go obfuscation rewrites a readable .go source file into a form that is harder for humans to follow while compiling to exactly the same behaviour. This tool uses a real Go tokenizer with lightweight scope analysis — not regex guessing — to rename only identifiers it can confirm are local to a single function, and it only encodes string literals it can prove are safe to touch (skipping import paths, const values, and struct tags, all of which Go requires to stay literal).
Go's scoping rules are stricter than Java's or JavaScript's in one important way: an unexported (lowercase) top-level function, type, or variable is still visible across every file in its package, not just the file it's declared in. A single-file tool can't safely verify what other files in the package reference, so this tool deliberately never renames anything at package level — only identifiers it can prove are local to one function body.
Table of Contents
What is Go Obfuscation?
Go obfuscation transforms a .go file into an equivalent program that is harder to read, modify, or reverse engineer, while producing the same results and side effects. Because a .go file is plain readable text, anyone with the source can open it and follow the logic unless it has been obscured first.
This tool focuses on source-level obfuscation: identifier renaming and string hiding, applied before go build. It's a different layer from binary-level protection, and a lighter-weight alternative to package-wide, build-integrated tools like garble when you just need to protect a single file.
Techniques Used by This Tool
Scope-Aware Local Renaming
Every local variable declared with := or var, every function and method parameter, named return value, and method receiver is identified and given a fresh name that's unique across the whole file. Because a name reused in a nested block, closure, or goroutine inside the same function still can't collide with a different binding of the same spelling, every matching occurrence throughout that function — including inside if/for blocks, closures passed to go or defer, and type switches — can be renamed consistently without changing what binds to what.
String Encoding with Matching Decoder
String literals are encoded as hex bytes XORed with a fresh random key per string. Two small decoder functions are inserted right after the import block. Strings used as import paths, inside const declarations, or in struct-tag position (immediately after a field's type, like `json:"name"`) are detected and left as plain literals, because Go requires those positions to be literal or constant.
Comment Removal
The tokenizer distinguishes real // and /* */ comments from string, rune, and raw string literals, so a comment-like sequence inside a string or backtick-quoted raw string is never mistaken for a comment.
ASI-Safe Whitespace Minification
Go statements are normally ended by a newline through automatic semicolon insertion (ASI), not an explicit ;. Stripping newlines naively would silently merge statements together. This tool implements the same rule the Go compiler uses: whenever a newline being removed follows a token that would have triggered ASI (an identifier, a literal, or one of break continue fallthrough return ++ -- ) ] }), a real semicolon is inserted in its place.
How the Tool Works
- Tokenize. A hand-written tokenizer splits the source into identifiers, keywords, interpreted and raw strings, rune literals, numbers, punctuation, and comments.
- Map structure. A lightweight structural pass walks every
{ }region and classifies it as a function/method body, a struct or interface type body, or a plain block (if/for/switch/select/closure) — correctly handling an inlineinterface{}orstruct{...}type showing up in the middle of a signature without corrupting the classification of the real function body around it. - Collect local declarations. Within each top-level function body (including everything nested inside it — closures, goroutines, deferred functions), declarations are found via three patterns: names immediately before
:=, names followingvar(single-line or groupedvar ( )blocks), and parameter/return/receiver names infuncheaders (using Go's own "either every parameter is named, or none are" rule to tell a name from a bare type). - Encode strings. Each string literal (outside import paths,
constvalues, and struct tags) is replaced with a call to the decoder, using the real escaped value so Unicode and escape sequences round-trip correctly. Unlike Java or C, Goswitchcase values don't need to be compile-time constants, so case-label strings are encoded normally. - Remove comments. Comment tokens are dropped before reconstruction.
- Reconstruct source. Tokens are re-emitted with their original spacing (or minimal merge-safe spacing plus ASI-correct semicolons, if minifying), and the decoder functions plus optional banner are inserted after the import block / package line.
- Validate structurally. The result's braces, parentheses, and brackets are re-counted to confirm they balance before it's shown as a success.
Output Validation
After transforming, the tool re-scans the output and confirms every {}, (), and [] is balanced, and that nothing was left structurally unclosed.
go build running in your browser. The tool cannot catch every possible edge case in unusual code. Always build and test the obfuscated file in your own environment before deploying it.What's Safe vs Unsafe to Rename
| Construct | Renamed? | Reason |
|---|---|---|
Local variables (:= / var) | Yes | Scoped to the enclosing function; not observable outside it |
| Function & method parameters | Yes | Bound at call time, not part of the exported signature's identity |
| Named return values | Yes | Local to the function, even when used by a naked return |
Method receivers (r *T) | Yes | Just a parameter name for the method body |
Generic type parameters [T any] | No | Structural, not local data — left untouched |
| Package-level funcs, vars, consts, types | No | Unexported names are still visible to every file in the package |
| Struct field names (any visibility) | No | Read via reflection, JSON/struct tags, and other files |
| String literals (general) | Encoded | Hidden behind a runtime decoder call |
String literals in switch cases | Encoded | Go case values don't need to be compile-time constants |
| Import path strings | No | Must be a literal, resolvable import path |
const string values | No | Go constants must be initialized with constant expressions |
Struct tags `json:"x"` | No | Must be a literal string token at that grammar position |
Use Cases
Distributed CLI Tools & Utilities
Share a single-file Go utility or internal CLI with clients without handing over fully readable internal logic.
Self-Hosted & Agent Binaries
Make the source behind a distributed agent, licensing check, or monitoring binary harder to read before building.
CodeCanyon / Marketplace Scripts
Obfuscate the core logic of a commercial Go script before packaging it for sale, while keeping package-level names intact for safe multi-file drop-in.
Game Servers & Anti-Cheat Logic
Make client-facing game server logic (loot tables, pricing, validation rules) harder to read or patch.
Limitations & Legal
- Not encryption. The decoder functions ship in the same file, so obfuscation is reversible with effort. Never embed real secrets, API keys, or passwords in distributed code.
- Single-file scope by design. Package-level (exported or unexported) functions, types, variables, and constants are never renamed, specifically so the output stays safe to drop back into a multi-file package.
- Struct fields are never renamed, to keep reflection, JSON/encoding tags, and cross-file struct literals intact.
- Unnamed parameter lists using only package-qualified types (rare, e.g. a bare
func(fmt.Stringer, io.Reader)type with no parameter names) are conservatively left alone to avoid any ambiguity with named parameters. - No real compiler check. The validator is a structural balance check, not
go build— always build and test the output yourself. - Legal. Only obfuscate code you own or have rights to modify. Do not use this to violate an open-source license.
Related Obfuscators
Frequently Asked Questions
What does a Go obfuscator do?
Is this Go obfuscator free?
Is my Go source code uploaded anywhere?
Why are exported (capitalized) names, package-level functions, and struct fields never renamed?
Will the obfuscated code still build?
Does string encoding ever break struct tags, const declarations, or imports?
What does string encoding do?
Does minification break Go's automatic semicolon insertion?
Can obfuscated Go be reversed?
Does it work with generics, goroutines, closures, and defer/recover?
Can I use this on a whole module or package, not just one file?
Does obfuscation affect performance?
Can I use this commercially?
Does this insert anything suspicious that would trigger antivirus or code-review flags?
Conclusion
This tool gives honest, source-level Go obfuscation with a structural validation pass — renaming only what it can prove is local and safe, and clearly documenting what it leaves alone at package level. Combine it with proper secret management, and, for whole-package/whole-binary protection, a build-integrated tool like garble.