}

Fix: "function main is undeclared in the main package" in Go

command-line-arguments runtime.main_main·f: function main is undeclared in the main package

This error occurs when Go cannot find a func main() in the main package. The most common cause is a missing or misspelled main function.

The Full Error

When you try to build or run a Go program and the entry point cannot be found, you see:

runtime.main_main·f: function main is undeclared in the main package

Or in some Go toolchain versions it appears as:

$ go run .
runtime.main_main·f: function main is undeclared in the main package
goroutine 1 [running]:
runtime.main()
    /usr/local/go/src/runtime/proc.go:255 +0x207
exit status 2

Or via go build:

$ go build ./...
command-line-arguments: runtime.main_main·f: function main is undeclared in the main package

Go requires every executable program to have exactly one func main() inside a file that declares package main. If the linker cannot find it, you get this error.


Cause 1: Missing func main() Entirely

The simplest cause — you forgot to write a main function.

Broken code:

package main

import "fmt"

func greet(name string) {
    fmt.Println("Hello,", name)
}

// No main function — nothing to call greet()!

Fixed code:

package main

import "fmt"

func greet(name string) {
    fmt.Println("Hello,", name)
}

func main() {
    greet("world")
}

Every Go executable must have a func main() — it is the entry point the runtime calls when the program starts.


Cause 2: Capital M — func Main() Instead of func main()

Go is a case-sensitive language. Main, MAIN, and main are three completely different identifiers. Only the lowercase func main() is recognized as the program entry point.

Broken code:

package main

import "fmt"

// Capital M — Go does NOT treat this as the entry point
func Main() {
    fmt.Println("This will never run automatically")
}

Fixed code:

package main

import "fmt"

// Lowercase m — this is the real entry point
func main() {
    fmt.Println("This works!")
}

This is one of the most common mistakes for developers coming from languages like Java (public static void Main) or C# (static void Main). In Go it must always be lowercase main.


Cause 3: Wrong Package Name — Not package main

Every .go file in a runnable program must start with package main. If you accidentally name the package something else (like package app, package util, or even package Main), Go will not treat it as an executable entry point.

Broken code:

// Wrong package declaration!
package app

import "fmt"

func main() {
    fmt.Println("Hello, world")
}

Running go run main.go on this file produces:

go run: cannot run non-main package

Or if other files in the directory declare package main, the linker error surfaces:

runtime.main_main·f: function main is undeclared in the main package

Fixed code:

// Correct: must be package main for executables
package main

import "fmt"

func main() {
    fmt.Println("Hello, world")
}

Key rule: package main is special in Go. It signals to the toolchain that this package should be compiled into an executable binary, not a reusable library.


Cause 4: Multiple Files in the Same Directory with Different Package Names

All .go files in the same directory must belong to the same package. If you mix package main with another package name in the same directory, the compiler cannot resolve the entry point.

Directory layout that breaks:

myapp/
├── main.go       (package main)
├── helper.go     (package utils)   ← WRONG
└── config.go     (package main)

helper.go (broken):

// This file accidentally uses the wrong package name
package utils

func HelperFunc() string {
    return "helping"
}

The error you get:

can't load package: package .: found packages main (main.go) and utils (helper.go)

Sometimes this also produces the function main is undeclared error depending on how you invoke Go.

Fixed helper.go:

// All files in the same directory must share the same package name
package main

func HelperFunc() string {
    return "helping"
}

Or, if HelperFunc truly belongs in a separate package, move it to its own subdirectory:

myapp/
├── main.go           (package main)
├── config.go         (package main)
└── utils/
    └── helper.go     (package utils)  ← now correct

Cause 5: Trying to Run a Library Package with go run

If you are working on a library (a package intended to be imported, not executed), it will never have a func main() because it does not need one. Running such a package directly with go run will always fail.

Example library file:

// This is a library — no main function, and that is correct for a library.
package stringutils

import "strings"

func Reverse(s string) string {
    runes := []rune(s)
    for i, j := 0, len(runes)-1; i < j; i, j = i+1, j-1 {
        runes[i], runes[j] = runes[j], runes[i]
    }
    return string(runes)
}

func ToTitle(s string) string {
    return strings.ToTitle(s)
}

Running go run stringutils.go on this gives:

runtime.main_main·f: function main is undeclared in the main package

The fix: Libraries are not run directly. Write a separate main.go that imports and uses the library:

// main.go — the runnable entry point
package main

import (
    "fmt"
    "mymodule/stringutils"
)

func main() {
    fmt.Println(stringutils.Reverse("hello"))  // prints: olleh
}

Then run the main file: go run main.go or go run .


Quick Diagnosis Checklist

Use this table to pinpoint the cause in your own code:

Check What to look for How to fix
Is there a func main()? Search the file for func main() Add func main() { ... }
Is it lowercase? func main not func Main or func MAIN Change to lowercase func main
Does the file start with package main? First non-comment line in file Change package declaration to package main
Do all files in the directory use the same package name? Check each .go file's first line Move mismatched files to a subdirectory
Are you trying to run a library? Does the package export functions but have no main? Create a separate main.go entry point

How to Properly Structure a Minimal Go Program

Here is the smallest valid Go program that compiles and runs:

package main

func main() {
}

That is it. Two lines of actual code. The file must:

  1. Declare package main at the top
  2. Contain exactly one func main() with that exact spelling

A more realistic minimal program that prints output:

package main

import "fmt"

func main() {
    fmt.Println("Hello, Go!")
}

Save it as main.go, then:

go run main.go
# Output: Hello, Go!

Or compile it to a binary first:

go build -o myprogram main.go
./myprogram
# Output: Hello, Go!

Multi-File Program Structure

When your program grows across multiple files, all files in the same directory share the same package:

myapp/
├── go.mod
├── main.go
├── users.go
└── config.go

main.go:

package main

func main() {
    cfg := loadConfig()
    runServer(cfg)
}

config.go:

package main  // same package as main.go

import "os"

func loadConfig() Config {
    return Config{
        Host: os.Getenv("HOST"),
        Port: os.Getenv("PORT"),
    }
}

type Config struct {
    Host string
    Port string
}

users.go:

package main  // same package as main.go

import "fmt"

func runServer(cfg Config) {
    fmt.Printf("Running on %s:%s\n", cfg.Host, cfg.Port)
}

Run the whole directory with: go run .


Summary

The error runtime.main_main·f: function main is undeclared in the main package always means the Go linker could not find the program entry point. The five root causes are:

  1. Missing func main() — add it
  2. Wrong capitalisationfunc Main() must be func main()
  3. Wrong package name — the file must start with package main
  4. Mixed package names in one directory — all .go files in a directory must share the same package name
  5. Running a library with go run — libraries have no main; create a separate entry point

In every case, the fix is straightforward once you know which cause applies. Use the checklist table above to narrow it down in under a minute.