donglin9068 2015-04-16 14:28
浏览 850
已采纳

Go项目中需要main.go吗?

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?

  • 写回答

2条回答 默认 最新

  • doushi1510 2015-04-16 14:32
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?