Advanced Error Handling in Golang

If you have ever written any Golang code you have probably noticed the built-in error type interface. Golang uses error values to indicate an abnormal state. The error type represents any value that can describe itself as a string. Here is the interface’s declaration: type error interface { Error() string } The most commonly-used error implementation is the errors package’s implementation that allows you to instantiate errors by using the following code snippet:...

September 25, 2017 · 2 min · Svetlin Ralchev

Network Programming and Proxies in Golang

Have you used any proxy servers in your infrastructure? There are a lot of different scenarios in which you may use a proxy in order to control access to your machines and resources. I am not saying that it’s the best approach but some companies use that. Recently, I have been working on a TCP service that has to connect via SOCK5 proxy server. But before we dig into that, I would like to show what Golang offers for the regular HTTP user....

August 22, 2017 · 3 min · Svetlin Ralchev

Golang: Extending reflect.StructTag to support duplicates

Presently, Golang has limited support of reflection comparing to the mainstream languages like C# and JAVA. It’s not intended to match or beat that languages. In practice, we are using StructTag to add some metadata for the defined struct fields. Such an example is json package, where you can customize the field marshaling. In example below, we customize the json representation of User struct fields: type User struct { ID string `json:"id"` Name string `json:"name"` CreatedAt time....

March 21, 2017 · 3 min · Svetlin Ralchev

Design Patterns in Golang: Decorator

Introduction The Decorator pattern adds new functionality to an existing object without altering its structure. It is a structural pattern as this pattern acts as a wrapper to existing class. The instanciate a decorator struct which decorates (wraps) the original object and provides additional functionality keeping its methods signature intact. Purpose Attach additional responsibilities to an object dynamically. Decorators provide a flexible alternative to inheritance for extending functionality. Wrapping a present, putting it in a box, and wrapping the box....

April 17, 2016 · 3 min · Svetlin Ralchev

Design Patterns in Golang: Composite

Introduction A Composite Design Pattern is a structural pattern that uses to employ composition when implementing an interface rather than using multiple inheritance. It composes objects into tree structures and lets clients treat individual objects and compositions uniformly. There are situations when clients ignore the difference between compositions of objects and individual objects. If dealing with multiple objects in the same way as handle each of deam is identical, the Composite Design pattern is a good choice to decrease the complexity and treat them as homogeneous....

March 31, 2016 · 3 min · Svetlin Ralchev