Understanding Your X-Go App

The Entry Point: main.go

Like every Go application, the entry point is main.go. Here you’ll find two key steps:

  1. Build - The app builds itself and especially its services container
  2. Run - Executes a command passed via argument (generally web to start the app as a web application)

Injectable Services: The Cornerstone

In an X-Go app, (almost) everything is an injectable service. Injectable services are the cornerstone of the X-Go architecture.

Here’s an example of an injectable service:

package fetchers

import (
    // [...]
)

type StoryFetcher struct {
    Fetcher    *fetcher.Fetcher
}

func (this *StoryFetcher) FindAll(ctx context.Context) ([]*mdl.Story, error) {
	// [...]
}

In this service StoryFetcher from file story_fetcher.go, we have injected another service Fetcher. If you declare a service available to the container in the main structure of a service file, it will be automatically injected during the build phase. This is the only “magic” case in the whole framework.

File Annotations

All Go files in an X-Go project have an annotation just below the package declaration, like // |@@| C.

If You Don’t Use Gen-Code

You can safely strip these annotations - they won’t affect your application.

If You Use Gen-Code

You must keep and manage these annotations. They control how Gen-code handles your files during sync operations.

Annotation Types

// |@@| C (Controlled) - This file is controlled by Gen-code - Do not edit - all your changes will be lost during the next Gen-code sync down

// |@@| F (Free) - File content is free to modify - You can modify function or structure contents as long as you keep the public function signatures

// |@@| W (Write-protected) - This file will be overwritten by your version when you sync down from Gen-code - Your changes are guaranteed to stay, whatever Gen-code does

Changing Controlled Files

To modify a // |@@| C file: 1. Change the annotation from // |@@| C to // |@@| W 2. Now you can freely modify its content

Warning: Since the file is no longer regenerated, it can break during sync down. For example: - Your // |@@| W code uses GardenFetcher->FindSet() - The fetcher (controlled via // |@@| C) loses this function during sync down - Your // |@@| W code will break, causing compilation errors

Next Steps

Now that you understand how X-Go apps are structured with injectable services, let’s explore how to handle web requests:

Routing - Learn how to define routes and handle HTTP requests