A tool that enables running Go files as scripts with inline dependency declarations, inspired by Python's inline script dependencies (PEP 723), uv's run command, and this Hacker News discussion.
Go is a compiled language that typically requires a full project structure with go.mod files for dependency management. This tool allows you to write self-contained Go scripts with dependencies declared inline, making Go more suitable for quick scripts and automation tasks.
- Run Go files directly with inline dependencies
- No need for a separate
go.modfile - Automatic dependency resolution via
go mod tidy - Shebang support for executable scripts
- Temporary build environment (doesn't pollute your workspace)
- Support for both running scripts and testing them
go install github.com/imjasonh/gos# Run a Go script
gos run script.go [args...]
# Run tests in a Go script
gos test script.go [args...]Add this to the top of your Go file:
#!/usr/bin/env gos runFor test files:
#!/usr/bin/env gos testThen make it executable:
chmod +x script.go
./script.go [args...]Note: Adding a shebang makes the file invalid Go syntax and may break some tools like gofmt and IDE formatting. The shebang is optional - you can always use gos run script.go instead.
Dependencies are declared in a special comment block at the top of your Go file:
#!/usr/bin/env gos run
// /// script
// dependencies = [
// "github.com/fatih/[email protected]",
// "github.com/spf13/[email protected]",
// ]
// ///
package main
import (
"github.com/fatih/color"
// ... your imports
)
func main() {
color.Green("Hello from gos!")
}#!/usr/bin/env gos run
// /// script
// dependencies = [
// "github.com/fatih/[email protected]",
// ]
// ///
package main
import (
"fmt"
"os"
"github.com/fatih/color"
)
func main() {
color.Green("✓ Hello from gos!")
fmt.Printf("Arguments: %v\n", os.Args[1:])
}You can have both a runnable script and tests in the same file:
#!/usr/bin/env gos run
// /// script
// dependencies = [
// "github.com/stretchr/[email protected]",
// ]
// ///
package main
import (
"fmt"
"testing"
"github.com/stretchr/testify/assert"
)
func main() {
fmt.Println("Running main function")
}
func TestSomething(t *testing.T) {
assert.Equal(t, 1, 1)
}Then:
gos run script.goor./script.go- runs the main functiongos test script.go- runs the tests (automatically renames to*_test.go)
gosparses the special comment block to extract dependencies- Creates a temporary directory with a generated
go.mod - Runs
go mod tidyto fetch dependencies - For
run: builds and executes your script - For
test: renames to*_test.goif needed and runsgo test - Cleans up the temporary directory
- Python + uv: Python's PEP 723 allows inline script dependencies, and uv's
runcommand executes them - Deno: Supports URL imports and can run TypeScript directly
- Bun: Can run TypeScript files with automatic dependency installation
- gos: Brings similar convenience to Go while maintaining Go's type safety and compilation
- Dependencies must be explicitly versioned or use
latest - No caching of built binaries (rebuilds each time)
- Requires
goto be installed and available in PATH - Test files must contain valid test functions for
gos testto work
- Binary caching for faster repeated runs
- Support for
go.sumverification - Additional commands beyond
run - Dependency caching across scripts
- Support for replace directives
