golang/go · error · PackageError
named files must be .go files: %s
Error message
named files must be .go files: %s
What it means
Returned by GoFilesPackage at line 3283. This function handles the `go run`/`go build` case where individual .go files are passed on the command line. Every argument must end in `.go`; the first one that does not causes a synthesized Package whose Error is this message and whose Name is the bad argument. The check is a pure suffix test on the file name.
Source
Thrown at src/cmd/go/internal/load/pkg.go:3283
p.Internal.Ldflags = BuildLdflags.For(ld, p)
p.Internal.Gccgoflags = BuildGccgoflags.For(ld, p)
}
}
// GoFilesPackage creates a package for building a collection of Go files
// (typically named on the command line). The target is named p.a for
// package p or named after the first Go file for package main.
func GoFilesPackage(ld *modload.Loader, ctx context.Context, opts PackageOpts, gofiles []string) *Package {
modload.Init(ld)
for _, f := range gofiles {
if !strings.HasSuffix(f, ".go") {
pkg := new(Package)
pkg.Internal.Local = true
pkg.Internal.CmdlineFiles = true
pkg.Name = f
pkg.Error = &PackageError{
Err: fmt.Errorf("named files must be .go files: %s", pkg.Name),
}
pkg.Incomplete = true
return pkg
}
}
var stk ImportStack
ctxt := cfg.BuildContext
ctxt.UseAllFiles = true
// Synthesize fake "directory" that only shows the named files,
// to make it look like this is a standard package or
// command directory. So that local imports resolve
// consistently, the files must all be in the same directory.
var dirent []fs.FileInfo
var dir string
for _, file := range gofiles {
fi, err := fsys.Stat(file)View on GitHub (pinned to b6b368adc5)
Solutions
- Append `.go` to the argument: `go run main.go`.
- Filter the file list to only `*.go` before passing it to `go run`/`go build`.
- If you meant to build a package, pass the import path or package directory instead of individual files.
Example fix
# before go run main # after go run main.go
Defensive patterns
Strategy: validation
Validate before calling
// Validate that every file argument ends in .go before invoking go run/build.
package argcheck
import (
"errors"
"strings"
)
func AllArgsAreGoFiles(files []string) error {
for _, f := range files {
if !strings.HasSuffix(f, ".go") {
return errors.New("not a .go file: " + f)
}
}
return nil
} Prevention
- Always pass the `.go` extension in `go run` invocations.
- Filter shell globs (`*.go`) before forwarding to `go run`.
When it happens
Trigger: Invoking `go run foo.txt`, `go build main`, `go vet app.go README.md`, or passing a directory/binary/script as if it were a .go file. Even an empty extension (`go run README`) fails.
Common situations: Typing `go run main` instead of `go run main.go`; shell glob expanding to include a non-.go file; passing a build script by mistake; tutorials that drop the extension.
Related errors
- tool %q is ambiguous; choose one of:
- C compiler %q not found: %v
- Fortran source files not allowed when not using cgo or SWIG:
- main package is in repository %q but current directory is in
- main module is in repository %q but current directory is in
AI-assisted analysis of golang/go@b6b368adc5 (2026-08-12).
Data as JSON: /api/errors/68361f8ca83a1419.
Report an issue: GitHub.