Search code examples
go

Is main.go required in a Go project?


Without a background in C and only "beginner" experience in Go I'm trying to work out whether main.go is actually required or is just a convention.

I'm looking to create a simple web API but could someone clarify this for me?


Solution

  • main.go as a file is not required.

    However, a main package with a func main() is required for executables.

    Your file name can be called whatever you want.

    E.g

    myawesomeapp.go

    package main
    
    func main() {
      fmt.Println("Hello World")
    }
    

    Running go run myawesomeapp.go will work as expected.

    See Program Execution from the Go language specification.