Can I use a main function with Encore?

Hey all, I’m new here so sorry if this is a stupid question!

I’m trying to deploy an existing project using Encore and I’m wondering what to do with my main function.

None of the examples in the tutorial section have main functions so assume I can’t have one?

Would be good to know what’s the recommended approach here.


Hey @Cowboy127 – Great question, this has now been clarified in the docs!

For Encore apps you don’t write a main function, because Encore automatically generates it for you to initialize all your infrastructure resources when the application starts up. You just define each service as a Go package with an API definition. (See docs)

If you want to customize the service initialization behavior, you can use a service struct. (See docs)

So for your existing project, if it’s a single service application you can simply rename the main function to something else. Just make sure to define at least 1 API endpoint in the service in order for Encore to understand that it should be created as a service.

E.g. hello.go would look like this, and Encore will create this as a service (hello) with an API endpoint (Ping).

package hello // service name

//encore:api public
func Ping(ctx context.Context, params *PingParams) (*PingResponse, error) {
    msg := fmt.Sprintf("Hello, %s!", params.Name)
    return &PingResponse{Message: msg}, nil

Hope that helps, otherwise let me know!