Skip to main content

Coding Conventions

We follow the Effective Go guidelines as the foundation for our coding conventions. This document summarizes the most important practices and tools enforced in this project.


Naming

General Rules

  • camelCase for:
    • Local variables
    • Private functions and methods (unexported)
  • PascalCase for:
    • Exported functions
    • Types (structs, interfaces, etc.)
    • Constants that need to be exported

Examples

ContextExample
Variable nameinfrahubSync
Exported function nameGetSortedListByLabel()
Exported typeDynamicMulticlusterFactory
Unexported functionprocessArtifacts()

Formatting

All code must be formatted using the official Go formatting tool:

  • gofmt: Standard formatting tool from the Go toolchain.
    • Most editors apply this automatically.
    • Run gofmt -s -w . to format code and simplify syntax.

Linting

We use golangci-lint as the primary linting tool. It aggregates multiple linters, including staticcheck, govet, gocritic, and others.

Usage

To run linting locally:

make lint

Architecture

Use interfaces to define behavior and decouple components. This allows for easier testing and mocking.

Example

// Domain Layer
type DynamicMulticlusterFactory interface {
GetCachedClientFor(ctx context.Context, serverURL string, k8sClient client.Client) (client.Client, error)
}
// Adapter Layer
type DynamicMulticlusterFactory struct {
mu sync.Mutex
clients map[string]client.Client
}

func NewDynamicMulticlusterFactory() *DynamicMulticlusterFactory {
return &DynamicMulticlusterFactory{
clients: make(map[string]client.Client),
}
}

func (f *DynamicMulticlusterFactory) GetCachedClientFor(ctx context.Context, serverURL string, k8sClient client.Client) (client.Client, error) {
// Implementation details...
}