Code directory structure

How to organize a codebase for better maintainability? Let's find out.

Maintainability

Layer

Main

The main entrypoint creates infrastructure and service instances, injecting dependencies, and starts the handler.

Layer-based organization

cmd/
    main.go
internal/
    model/
        stock.go
        notification.go
    infrastructure/
        database/
            postgresql.go
            sqlite.go
    port/
        database.go
    service/
        stock.go
        notification.go
    handler/
        rest.go
        grpc.go
MetricMethod
SearchWorse

Functionality-based organization

cmd/
    main.go
internal/
    stock/
        model.go
        service.go
        port.go
    notification/
        model.go
        service.go
        port.go
    infrastructure/
        database/
            postgresql.go
            sqlite.go
    handler/
        rest.go
        grpc.go
MetricMethod
SearchBetter

References

←Previous Next→