Course Overview
Locked video preview

Paid video

Please purchase the course to watch.

Building the home view

Sending data to home view

Here is a summary of the video transcript:

  • Introducing Dependency Injection in Controllers
    The controller is refactored into a struct that accepts dependencies, specifically a database connection. A constructor function initializes the controller, enabling scalable and maintainable dependency injection.

  • Controller Struct and Constructor Pattern
    A Controller struct is created with a database dependency, and a New function returns an initialized instance. This pattern allows additional dependencies like caching or locking to be added cleanly in the future.

  • Reusable Render Helper Function
    A helper render method is implemented to reduce repetitive rendering logic. It renders a template component into a buffer and sends the resulting HTML response with an HTTP 200 status.

  • Refactoring the Home Controller Method
    The home handler is converted into a method on the controller struct. It retrieves articles using the injected database connection and uses the new render helper to return the home view.

  • Updating Router and Application Wiring
    The router is updated to use the new controller struct instead of standalone functions. The main.go file is modified to instantiate the controller with the database dependency and pass it into the router.

  • Application Execution and Result
    After wiring dependencies correctly, the application runs successfully. The home page renders properly, though no articles appear yet because none have been inserted into the database.