Common Patterns

X-Go follows consistent patterns throughout the framework to make your code predictable and maintainable.

Function Signatures

Context First

The first argument is always ctx context.Context. This allows proper request cancellation and deadline handling throughout your application. This pattern is also heavily used by the framework’s library services, especially for testing.

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

Error Last

The last return value is always err error. This follows Go’s idiomatic error handling pattern.

func ProcessStory(ctx context.Context, story *mdl.Story) (*Result, error) {
    // Implementation
    if err != nil {
        return nil, err
    }
    return result, nil
}