`main.go` is not mandatory — it’s just a common convention. what *is* required for an executable go program is a `main` package containing a `func main()` entry point, regardless of the filename.
In Go, the build and execution system doesn’t enforce any specific filename — only structural and naming requirements at the package and function level. As long as your source file declares package main and defines a func main() with no arguments and no return values, go run, go build, or go install will treat it as an executable program.
For example, this file named api-server.go is perfectly valid:
package main
import (
"fmt"
"net/http"
)
func main() {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hello from Go API!")
})
fmt.Println("API server starting on :8080")
http.ListenAndServe(":8080", nil)
}
You can run it directly with: