-
Unlocking Simplicity: Easy Concurrency with the Lock Design Pattern in Go
Introduction When we build programs that do many things at once, we want to make sure they’re secure. Go is a modern language that is really good at keeping things safe in memory but still works really fast. One big reason for Go’s safety is its “lock pattern.” This is like a special tool that…
-
Elegant and Easy: Unleashing the Power of the Composite Pattern in Go
Introduction The composite pattern allows you treat a group of objects like a single object. The objects are composed into some form of a tree-structure to make this possible. This patterns solves two problems: We do this by doing the following: This all sounds rather cryptic, so let us have a look at the diagram:…
-
Design Patterns in Go: Factory method, automating the factory
Introduction In this article I discussed the implementation of the Abstract Factory pattern. The Factory Method is simply an extension of that pattern. Creating an object can sometimes be complex. A factory-method can abstract this complexity away, and let subclasses or in our case interface implementations decide which objects to create. It looks like this:…
-
Design Patterns in Go: Prototype
Introduction The prototype-pattern is a creational design pattern that allows us to create new objects by cloning existing ones, i.e. the existing objects function as a kind of template. This can save time in some cases when for example an object creation involves some heavy calculation or things like network traffic or database queries. So…
-
Design Patterns in Go: Abstract Factory, the flexible production of objects
Introduction The Abstract Factory Pattern is a way to group the creation of related objects, like products of a certain brand or make by building a common factory interface. If this all sounds abstract, a picture can help: A short breakdown: This is a somewhat more complicated design pattern, but when used well can lead…
-
Design Patterns in Go: Flyweight, or go easy on your memory
Introduction The flyweight pattern is a pattern that helps minimize memory usage by sharing and reusing data. A common example is wordprocessor where each character not only holds the character like ‘A’ but also things like markup and formatting data. Instead of having each element of a document carry all this data, this markup and…
-
The Binding Properties Pattern in Go: Leveraging Generics for Clean Design
Introduction Go is known for its simplicity and clarity, but when it comes to managing state or creating reactive data flows, developers often have to write a lot of boilerplate. With the introduction of generics in Go 1.18, new patterns are emerging that make it easier to express reusable, type-safe abstractions. One such pattern is…
-
Decoding Design: Exploring the Specification Pattern in Go for Powerful Code Composition
Introduction Most applications require business rules, such as data validation. It’s crucial to implement these rules in a way that’s flexible, clear, and easy to maintain. The Specification pattern offers a solution, allowing the creation of reusable business rules that can be combined using boolean logic. Implementation in Go In this example, we’ll build a…
-
Demystifying the Read-Write Lock Pattern in Go: Simple Strategies for Easy Concurrency
Introduction In another article we discussed the Lock pattern. In this we used the sync.Mutex struct. The problem with this struct is, is that it doesn’t distinguish between reading from a resource, like accessing an element in a vector, and writing to it. However, if a resource is primarily read, having multiple readers waiting to acquire…
-
Concurrency Under Control: Channeling the Monitor Pattern in Go
Introduction Many times when writing concurrent code, the need arises to protect a certain resource, or resources from concurrent access. A way of doing this is by using the Monitor pattern. Implementing this pattern usually involves the use of the locks and condition variables. Implementing the pattern like this can be done in Go as well,…
-
Simple Implementation of the Monitor Object Pattern in Go for Easy Concurrency Control
Introduction Sometimes in a multi-threaded program, there is a need to protect a resource from concurrent access, that is access by multiple threads. One way of doing this is by implementing a Monitor Object. Implementing this pattern usually involves the use of the locks such as sync.Mutex and condition variables such as sync.Cond Basically a monitor is a…
-
Easy Concurrency: Harnessing the Reactor Pattern in Go
Introduction There are many ways to handle incoming events. If you need to be able to handle many potential service requests concurrently, the Reactor pattern could be your pattern of choice. The key ingredient of this pattern is a so-called event-loop, which delegates request to the designated requestr handler. Through the use of this mechanism,…
-
Simple Concurrent Join Pattern: Streamlined Coordination in Go
Introduction In some applications it is handy not to say necessary for the main thread (or a thread) to wait for the completion of several worker threads. Examples of these kinds of problems are: There are probably many other use cases, the common thing between these use cases is that part of the work is…
-
Simple Double Checked Locking in Go for Effortless Concurrency Control
Introduction Sometimes when locking data or objects it can be handy to reduce the overhead of acquiring a lock on such objects by first checking whether a lock is really necessary. This typically used in combination with lazy initialization in multi-threaded applications, sometimes as part of a Singleton pattern. In Go we can implement this…
-
Demystifying Concurrency: Simple Implementation of the Binding Properties Pattern in Go
Introduction Especially in multi-threaded applications it can be necessary to synchronize properties between objects, or at least be notified of changes on certain properties. This is where the Binding Properties comes in, which is basically a form of the Observer pattern. In this pattern, observers can subscribe to a special ‘event’-handler on an object. When…
-
Effortless Resource Management: A Simple Context Manager Implementation in Go
Introduction Many languages have the concept of a context manager. This is a way of efficiently and automatically cleaning up resources. To be fair, this is just a first attempt, please let me know if you have suggestion on how to improve on the code. The code and the package are on https://github.com/snoekiede/gocontextmanager C# C#…
-
Easy Decoding: Unraveling the Balking Pattern in Go for Effortless Mastery
Introduction The Balking Pattern might not be widely known, but it plays a crucial role in preventing certain actions on objects based on their state. For instance, attempting to read the contents of an unopened file should not be allowed. Advantages and disadvantages of the Balking Pattern The Balking Pattern has several advantages: The disadvantages…
-
Mastering Concurrent Harmony: Easy Implementation of the Guarded Suspension Pattern in Go
Introduction In multithreaded applications, it’s common for one thread to let another know when specific conditions are met, like when data is ready or a time-consuming task is finished. In Go, you can use goroutines and channels to achieve this. This example demonstrates implementing a GuardedGarage to park ExpensiveCar objects. Implementation in Go We will…
-
Mastering Go’s Event-Driven Brilliance: A Journey to Easy Asynchronous Excellence
Introduction Sometimes, when your program has a task that takes a lot of time, like working with databases, web services, or complex calculations, you might want to let it happen in the background. This way, your program can keep running smoothly without waiting for the time-consuming task to finish. In Go, we can achieve this…
-
Simplify Go Development: Harnessing the Power of Easy Fluent Interfaces for Effortless Coding
Introduction Using a fluent interface can make your code easier to read. This pattern allows you to connect method calls, as each method returns the object or context. When implemented well, it can create a kind of specialized language for a specific domain. Let’s see how this works in Go Implementation in Go For this…
Code nomad
Code Freely, Live Fully
Code nomad
Code Freely, Live Fully



















