Sobes.tech
Junior — Middle

Can you give examples of design patterns from the Gang of Four book that you have used in your projects?

sobes.tech AI

Answer from AI

In Go projects, I often used the following patterns from the Gang of Four book:

  • Singleton — for creating a single instance of the application configuration or logger.
  • Factory Method — for creating objects with different implementations of an interface, such as various types of storage (file-based, in-memory, database).
  • Decorator — for dynamically extending functionality, such as wrapping a logger to add formatting or message filtering.
  • Observer — for implementing an event-driven model, such as subscribing to changes in an object's state.

Example of using the Singleton pattern in Go:

package config

type Config struct {
    DatabaseURL string
}

var instance *Config

func GetConfig() *Config {
    if instance == nil {
        instance = &Config{DatabaseURL: "postgres://localhost/db"}
    }
    return instance
}

This pattern ensures that the configuration is loaded and accessible as a single instance throughout the application.