{"record":{"id":"68361f8ca83a1419","repo":"golang/go","slug":"named-files-must-be-go-files-s","errorCode":null,"errorMessage":"named files must be .go files: %s","messagePattern":"named files must be \\.go files: (.+?)","errorType":"exception","errorClass":"PackageError","httpStatus":null,"severity":"error","filePath":"src/cmd/go/internal/load/pkg.go","lineNumber":3283,"sourceCode":"\t\tp.Internal.Ldflags = BuildLdflags.For(ld, p)\n\t\tp.Internal.Gccgoflags = BuildGccgoflags.For(ld, p)\n\t}\n}\n\n// GoFilesPackage creates a package for building a collection of Go files\n// (typically named on the command line). The target is named p.a for\n// package p or named after the first Go file for package main.\nfunc GoFilesPackage(ld *modload.Loader, ctx context.Context, opts PackageOpts, gofiles []string) *Package {\n\tmodload.Init(ld)\n\n\tfor _, f := range gofiles {\n\t\tif !strings.HasSuffix(f, \".go\") {\n\t\t\tpkg := new(Package)\n\t\t\tpkg.Internal.Local = true\n\t\t\tpkg.Internal.CmdlineFiles = true\n\t\t\tpkg.Name = f\n\t\t\tpkg.Error = &PackageError{\n\t\t\t\tErr: fmt.Errorf(\"named files must be .go files: %s\", pkg.Name),\n\t\t\t}\n\t\t\tpkg.Incomplete = true\n\t\t\treturn pkg\n\t\t}\n\t}\n\n\tvar stk ImportStack\n\tctxt := cfg.BuildContext\n\tctxt.UseAllFiles = true\n\n\t// Synthesize fake \"directory\" that only shows the named files,\n\t// to make it look like this is a standard package or\n\t// command directory. So that local imports resolve\n\t// consistently, the files must all be in the same directory.\n\tvar dirent []fs.FileInfo\n\tvar dir string\n\tfor _, file := range gofiles {\n\t\tfi, err := fsys.Stat(file)","sourceCodeStart":3265,"sourceCodeEnd":3301,"githubUrl":"https://github.com/golang/go/blob/b6b368adc57c96c3151d224d172029f233ead2c3/src/cmd/go/internal/load/pkg.go#L3265-L3301","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"# before\ngo run main\n# after\ngo run main.go","handlingStrategy":"validation","validationCode":"// Validate that every file argument ends in .go before invoking go run/build.\npackage argcheck\n\nimport (\n\t\"errors\"\n\t\"strings\"\n)\n\nfunc AllArgsAreGoFiles(files []string) error {\n\tfor _, f := range files {\n\t\tif !strings.HasSuffix(f, \".go\") {\n\t\t\treturn errors.New(\"not a .go file: \" + f)\n\t\t}\n\t}\n\treturn nil\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Always pass the `.go` extension in `go run` invocations.","Filter shell globs (`*.go`) before forwarding to `go run`."],"tags":["build","cli","toolchain"],"backgroundTag":null,"analyzedSha":"b6b368adc57c96c3151d224d172029f233ead2c3","analyzedAt":"2026-08-12T00:22:02.250Z","schemaVersion":2},"datasetVersion":"2026-08-12T13:17:24.610Z"}