
Explore design patterns in Go to write clean, maintainable, scalable code. See factory, abstract factory, repository, singleton, builder, fluent interface, and adapter patterns demonstrated through a pet breeds web app.
Meet a seasoned software professional with 25 years as an independent contractor and decades of university teaching, sharing insights on design patterns in Go.
Download the latest go from go.dev and install for your OS—windows, mac with apple silicon, mac intel, or linux—and use the linux commands rm rf and sudo when needed.
Install an integrated development environment to simplify Go development with Visual Studio Code, install the Go extension, run Go tools via the command palette, and enable Go template syntax.
Install Docker to run all microservices as containers across Mac, Windows, and Linux, avoiding separate binaries and port management; Docker is free and easy to install.
Search online for solutions, compare your code to the lecture source, and review the Q&A; share enough code or a link to your git repository for targeted assistance.
Explore common mistakes in software development in this intermediate course, as the instructor shows unedited errors and demonstrates fixing them.
Set up a basic go web app, implement routing and simple handlers, and build pages with go templates to explore design patterns in a real-world scenario.
Set up a simple web server, route requests through a software router to handlers, query a database, and return HTML or JSON via templates.
Set up a go web project with go mod init, build a simple http server in cmd/web/main.go on port 4000, and return hello world at root.
Install a third-party routing package to replace the default serve mux, using the Chai router (version five) and update your go.mod accordingly to enable route handling in later lectures.
Set up routing with chi v5 router, create a mux, add recover and timeout middleware, define routes and a show home handler, and run the http server on port 4000.
Learn to build dynamic HTML pages in Go by creating a base layout with header and footer, and reusable blocks for content, CSS, and JavaScript using Go templates and Bootstrap.
Create a render function in Go that builds and renders HTML templates with a template data struct and a cache-backed map, using a base layout and partials.
Render the first page by wiring a home page template to the base template, define the content block, and initialize the template map in Go to load partials.
Add a bootstrap navigation bar to the base layout so users can access the site across pages. Configure a fixed top dark navbar with home, about, and a cat breeds and a dog breeds dropdown, then wire up page stubs for navigation.
Create Go HTML templates for all site pages using a base template, offset the fixed top nav with CSS, and prepare pages for about, cat and dog breeds and breeders.
Set up a basic router with a dynamic /{page} route and a showPage handler that renders the corresponding page.go HTML template based on the URL parameter.
Explore how to implement and test a template cache in Go. Switch between cache reads and building templates from disk, using a command line flag to enable caching.
Learn to serve static images from a Go web app using a static folder as part of design patterns, with WebP and JPEG formats and fallback for older browsers.
Start your Go design patterns journey by exploring the factory pattern and the abstract factory pattern, creating objects with defaults and families without relying on concrete classes.
Explore the factory pattern in Go, a simple design pattern. Create a product with createdat and updatedat set to time.Now, and apply it to dog and cat breeders.
Create and organize Go models for a pet directory project, implementing dog and cat breed types, breeder and pet structs, and prepare for a factory pattern.
Explore a simple go factory pattern by building a NewPet function in the pets package that returns a models.pet with species like cat or dog, ready for a json route.
Create Go handlers that use a simple factory to instantiate new pets (dog or cat) and respond with JSON, using a helper toolbox to write JSON with http 200.
Set up a front-end test page with a base template, create routes for dog factory and cat factory, and implement a JavaScript fetch-based UI to display factory results in JSON.
Add routes to expose factory handlers, including test patterns and api endpoints for creating dogs and cats from factory, and render the test page template.
Run the Go web app, test the basic factory by triggering dog and cat species through a JSON response, and preview advancing to the abstract factory in the next lecture.
Explore the abstract factory pattern, a creational design pattern that creates families of related objects, decoupling client code from concrete implementations by producing dogs and cats.
Implement the abstract factory pattern in Go by defining an animal interface and concrete factories for dogs and cats, each embedding models and exposing a show method.
Create two handlers for the abstract factory to return a dog and a cat as json, wired to routes api/dog from abstract factory and api/cat from abstract factory.
Updates the front end for the Go abstract factory demo by adding html buttons for dog and cat, and javascript to fetch and display results from abstract factory endpoints.
Run the web app to test the abstract factory's dog and cat routes, validating embedded pet defaults in json, and noting the beginnings of MariaDB integration and the repository pattern.
Set up and connect a Go web application to a MariaDB database in Docker, providing a working database environment for exploring the repository pattern in the next section.
Learn how to add MySQL/MariaDB support in Go by installing the go-sql-driver/mysql driver and updating go.mod, with Docker-based database setup in the next lecture.
Set up a local MariaDB with Docker using docker-compose.yml and breeders_MySQL.sql, map port 3306, and launch with docker compose up -d to initialize the breeders database.
Connect a Go application to MariaDB by configuring a DSN, importing the MySQL driver, and initializing a db pool with timeouts, then verify the connection with a ping.
Explore the repository pattern as an adapter between business logic and data storage, enabling easy database switching (MariaDB, Oracle, Postgres, Mongo) and simpler unit testing of handlers.
Create a go database function that returns all dog breeds from a connection pool, using a context timeout and scanning into dog breed models for repository pattern and a handler.
Connect the database pool to the dog breed model and implement a get all dog breeds handler that returns the list as JSON via a dedicated API route.
Update the front end to fetch the dog breeds from /api/dog-breeds on DOMContentLoaded, parse JSON, and prepare to render as an HTML table.
Fetch data from the back end and render it in a searchable, paginated Bootstrap table on the dog breeds page, using simple data tables for a polished, responsive front end.
Write a test for the get all dog breeds json handler by creating a request, recording the response, and verifying a 200 status while outlining test setup and repository pattern.
Implement the repository pattern in Go by replacing the db field with a repository, defining a repository interface, a MySQL repository, and a factory function to create it.
Implement the repository pattern in Go by converting to a MySQL repository, wiring up a repository interface, and updating the model and main packages to use the new repo.
Remove the SQL db field from main.go and move the database into models, embedding the dog breed repository there. Next: start the database and run the app.
Start the database with docker compose, run the web app on port 4000, and verify the dog breeds page. Implement the database repository pattern to enable tests without a database.
Develop a test repository to run tests without a live database, mirroring the MySQL repository and adding dogs_test_db.go with a test repository implementation.
Update the handler tests to use a test repository when the database connection is nil, switching to a MySQL repository when a pool is provided.
Explore the singleton design pattern in Go by building a simple single-instance object to coordinate actions across a software system.
Implement a singleton configuration object to share a single database connection pool, using sync.Once and Getinstance to avoid exhausting connections across packages.
Update the Go project to use the configuration singleton, replace models with app, initialize app with DB, and adjust handlers and tests to reference the configuration.
Explore the builder pattern and the simple fluent interface in Go, demonstrating method chaining to construct objects with an example using pets.new builder to set species and breed.
Learn to implement the builder pattern and fluent interface in Go with method chaining to construct an address object using street, number, city, and country.
Explore the builder pattern in our Go project by defining a Pet type with JSON tags in the pets package and scaffolding builder.go to enable fluent construction.
Define a pet interface with setters for species, breed, min weight, max weight, description, lifespan, and age, then implement a build function that validates inputs and computes the average weight.
Fixes three problems in our builder code by adding the build function, removing set average weight, and changing age estimated to a boolean; prepares test handler for dog and cat.
Create a new http handler that uses the builder pattern with a new pet builder factory, populating species, breed, weight, color, age, and description, then return JSON or handle errors.
Set up a route to the handler by defining a mux.Get for /api/dog in routes.go and wiring it to the create dog with builder handler in Go.
configure the test page to call the builder route from the front end by wiring two buttons for dog and cat with JavaScript fetch calls and displaying the builder output.
implement a new cat endpoint by wiring a handler and route in the builder section, update front-end JavaScript, and practice typing the changes by hand rather than copying.
This lecture demonstrates implementing the builder pattern with a fluent interface in Go, showing a cat creator handler that builds a pet object, handles errors, and serves JSON responses.
Discover the adapter pattern in Go, enabling a web app handler to handle data from JSON or XML sources by transforming XML into the expected format without modifying existing code.
Master the adapter pattern in Go by implementing a data interface and adapters that switch between JSON and XML from a remote service.
Connects to JSON and XML services using the adapter pattern, builds adapters, and runs a minimal Go web app that serves cat data in both JSON and XML formats.
Apply the adapter pattern in Go to fetch cat breed data from remote service, first via a JSON back end, then via an XML adapter, using a cat breeds interface.
Embed the json adapter in the application config to centralize changes and avoid updating hundreds of handlers. Set up the json backend and bind it to app.catService in main.go.
Set up a Go http handler that uses an adapter pattern to fetch all cat breeds from the cat service and return them as JSON.
Add the cat breeds route, fix the adapter URL, run the web and api services, verify json at localhost:4000/api/cat/breeds, and prepare frontend updates with the cat breeds page template.
Replace the cat breeds page html and assets in the static folder, mirroring the dog breeds setup and updating the api call to /api/cat-breeds.
Explore how the adapter pattern in Go enables switching from json to xml without front end changes by adding xml tags and an xml back end for cat breeds.
Write tests using the adapter pattern to simulate the cat breeds handler, creating a test back end and test adapter to run Go test in isolation.
We build an abstract factory to fetch animal data from a remote source using the adapter pattern and a singleton, first in JSON, then in XML, illustrating maintainable design.
Create a stub animal from Abstract Factory handler that returns JSON based on species and breed from the URL, embedding breed data from local and remote sources.
Create a new pet with breed from the abstract factory, returning a dog or cat with the breed embedded and sketching adapters for a remote service before refactoring.
Modify the new pet with breed factory to accept a breed and implement it for dog and cat abstract factories, using a singleton app to fetch breed data via repository.
Add a get breed by name method to the dog breed repository, implement it in mysql and test repos, and integrate with the abstract factory.
Refactor adapters.go into a new adapters package so the abstract factory in the pets package can access adapters that fetch remote service data in XML or JSON.
Implement getCatBreedByName in the json adapter, fetch the breed via http request, unmarshal the json into a models.cat breed, and return its pointer.
Add a Get cat breed by name method to the XML adapter, mirroring the JSON adapter, update the interface, unmarshal the response into a models.CatBreed, and ensure proper resource cleanup.
Finish the new adapter and abstract factory code in Go by centralizing adapters, wiring cat service to a remote service, updating configuration and tests, and ensuring the project compiles.
Finish the abstract factory changes to use the adapters by wiring a cat factory with breed information, embedding services and repository from configuration, and handling errors when adding cat breed.
Set up a mux route and stub handler to return a json object with a dog or cat and its breed, extracting species and breed from the URL at /api/animal-from-abstract-factory/{species}/{breed}.
Finish the animal handler by extracting species and breed from the url, creating a new pet via the abstract factory with the adapter pattern, handling errors, and returning JSON.
Explore the decorator pattern in Go, using composition to decorate a struct with extra data, illustrated by a dog of the month with a picture.
Set up a get route /dog-month and a stub handler to demonstrate the decorator pattern, wiring routes.go and handlers.go planning to fetch and decorate a dog before serving the page.
Download and set up the dog of the month table from the provided dom.sql.zip, boot the database with Docker Compose, and implement code to access data from this table.
Implement a get dog of month by id method to access dog_of_month table, define dog_of_month with embedded dog, video and image fields, referencing the decorator pattern, and wire repository calls.
Fetch the breed by name, retrieve the dog of the month by ID, decorate the dog model with fields, parse birth date, and render the dog-month page with the data.
Create a dog of the month Go HTML template that binds dog data to the base layout, displays the static image, and adds the /dog-month route.
Go is a powerful language for building efficient and scalable applications. But as your projects grow, you'll encounter common problems that can be elegantly solved with design patterns.
This course will equip you with the knowledge and skills to leverage these design patterns effectively in your Go code. We'll explore various categories of patterns, including:
Creational Patterns: Learn techniques for object creation that promote flexibility and decoupling.
Structural Patterns: Discover ways to compose classes and objects to achieve desired functionality.
Behavioral Patterns: Explore patterns that define communication and interaction between objects.
By understanding these patterns, you'll gain the ability to:
Write cleaner, more maintainable, and reusable code.
Improve the design and architecture of your Go applications.
Solve common programming challenges with proven solutions.
Communicate design concepts more effectively with other developers.
We'll approach design patterns with a practical lens, focusing on real-world Go scenarios. In fact, we'll build a simple web application that allows us to see how, why, and when you can use a particular design pattern to make your code more efficient, maintainable, and easy to understand. My goal is ensure that you'll gain hands-on experience implementing these patterns so that you can use them in your own projects.
So, whether you're a seasoned Go developer or just getting started with the language, this course will provide you with valuable tools to take your Go development skills to the next level.
Please note that this course requires you to download Docker Desktop from Docker. If you are a Udemy Business user, please check with your employer before downloading software.