Henry Du Blog

think digital, act analog

Golang Inversion of Control Design Pattern

Golang Inversion of Control IoC Introduction Inversion of Control (IoC) is a programming design pattern which follows Dependency Inversion principle (DIP). It achieves decoupling the control logic and business logic. For instance, we normally will add control logic in the business logic, because it is very common in terms of human beings thinking logic. For example, we may add toggle-switch object when implement light bulb. We may also implement customized toggle-switch when making another electric device.

Golang Decorator Pattern

Golang Decorator Pattern Decorator pattern is the one of twenty-three well-known design patter from Gang of Four design patterns. It is categorized in structural design pattern, along with Adapter pattern, Proxy pattern etc. The decorator pattern allows behavior to be added to an individual object, dynamically, without affecting the behavior of other objects from the same class. It tries to resolve the problem that single responsibility, following the Single Responsibility Principle, should be added to or removed from an object dynamically at run-time.

Golang Interface Source Code Brief Look

Golang Interface Source Code Brief Look Based on go v1.13, there are two struct definition for go interface: eface and iface. type eface struct { _type *_type data unsafe.Pointer } type iface struct { tab *itab data unsafe.Pointer } eface The eface represents the interface which does not have any method: interface{}. The eface has two fields: _type and data. data is actually a pointer to point to real data. _type is defined as follows:

Add Subtract Coding Problem

Add Subtract Coding Problem Problem Write a function addSubtract to meet the requirement that, it will alternately adds and subtracts carried parameters. For example: add_subtract(7) -> 7 add_subtract(1)(2)(3) -> 1 + 2 - 3 -> 0 add_subtract(-5)(10)(3)(9) -> -5 + 10 - 3 + 9 -> 11 Solution The idea is to have a addSubtract struct to keep tracking the last add/subtract result and the count of function calls. If the count is even number, then use subtract operation, otherwise, use add operation.

Golang Decoupling, Embedding and Exporting

Golang Decoupling, Embedding and Exporting Decoupling Golang does not have a concept of object-oriented programming (OOP), including the concept of class or a class-based inheritance. Rather, Golang has its own way to interpret the regular object-oriented languages - decoupling. In traditional OOP design, one class contains member variables, either public or private. It also contains member functions, either public or private. In order to reuse the class member function, another class has to inherit the class.