Obfuscate a Go program
Requires Codefend 3.3.0 or later for the recommended m_ preset.
This example transforms a small Go command's source before compilation. It keeps package main, the entry point, and imported APIs stable. A lowercase generated prefix preserves the helper's unexported naming.
1. Create the program
Install the Go toolchain and Codefend. Codefend runs on Node.js 18 or later, independently of Go.
npm install --global codefendCreate source/main.go:
package main
import "fmt"
func m_total(m_values []int) int {
m_sum := 0
for _, m_value := range m_values {
m_sum += m_value
}
return m_sum
}
func main() {
m_values := []int{12, 18, 12}
fmt.Println(m_total(m_values))
}Expected obfuscated source
With the configuration in the next step, Codefend produces:
package main
import "fmt"
func ox0(ox1 []int) int {
ox2 := 0
for _, ox3 := range ox1 {
ox2 += ox3
}
return ox2
}
func main() {
ox1 := []int{12, 18, 12}
fmt.Println(ox0(ox1))
}The program's expected output remains 42. Generated identifier numbers can change when the source or Codefend version changes.
The private helper and locals use m_; main, fmt, and Println remain unchanged. This program uses only the standard library.
2. Configure names and output
Create .codefendrc.json in the project root:
{
"generation": {
"inputDir": "source",
"outputDir": "release"
},
"transformation": {
"prefix": "ox"
},
"parser": {
"name": "startWithMAndUnderscore"
}
}The startWithMAndUnderscore preset selects this example's m_ names, leaving unrelated underscore names alone. See parser presets for the exact rule and why we recommend this convention.
The ox prefix is deliberately lowercase. Go uses an identifier's first character to determine whether a package-level name is exported. An uppercase replacement such as Ox0 would change an unexported helper into an exported name. See exported identifiers in the Go specification.
Keep release separate from source and reserve it for generated files: Codefend replaces that directory. Store binaries and other non-text build artifacts outside the input tree.
3. Run both programs
From the project root:
go run source/main.go
codefend --obfuscate
go run release/main.goBoth commands should print:
42The Go command can run a list of named Go files. This standalone standard-library example therefore does not require a go.mod file. For module-based applications, retain your module and workspace configuration and run the generated package in its intended module context. See the Go command documentation.
Larger Go applications
- Keep exported APIs, interface method names, imported package members, and externally consumed identifiers stable. A lowercase prefix preserves this example's visibility, but does not coordinate interface contracts.
- Struct tags and exported fields often define JSON, database, or reflection behavior. Do not mark those names unless all consumers are covered.
- Preserve package declarations, import paths, filenames with build constraints, and generated-code contracts.
- Run
go testagainst the generated application in the appropriate module. Check all packages and cross-file references before distributing it. - This is text matching rather than a Go parser. The default empty pool avoids current dictionary-allocation limitations, but case variants, longer names, and string contents still need review. See troubleshooting.
Generated source must still pass the Go compiler; Codefend is not a transformation of an already compiled Go binary.
Add Codefend to your workflow
Generate the source before your Go build. This standalone example runs directly; for a module-based project, retain its module context and point your normal build and tests at the generated package sources.
codefend --obfuscate
go run release/main.goIn CI, stop on a failed step and package only validated generated output. See build and deployment for the shared workflow.