Multiple Packages in Same Directory

found packages main (main.go) and utils (utils.go) in /path/to/dir

Quick Answer

Each directory in Go can contain only one package (excluding test files with _test suffix). Move files to separate directories or use the same package name.

Why This Happens

Go requires all non-test .go files in a directory to declare the same package name. If two files in the same directory have different package declarations, the compiler cannot determine which package the directory represents. Test files may use packagename_test for black-box testing.

The Problem

// main.go
package main

// utils.go
package utils // different package in same directory!

The Fix

// main.go
package main

// utils.go
package main // same package

// Or move utils.go to a separate directory:
// utils/utils.go
package utils

Step-by-Step Fix

  1. 1

    Identify the conflicting packages

    Read the error to see which files declare different package names.

  2. 2

    Decide on the package

    Determine which package name the directory should use.

  3. 3

    Move or rename

    Either change the package declaration to match, or move the file to its own directory.

Bugsly catches this automatically

Bugsly's AI analyzes this error pattern in real-time, explains what went wrong in plain English, and suggests the exact fix — before your users even report it.

Try Bugsly free