Status quo

The most powerful emotions demand to be felt.. “Status quo” is published by Keets.

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




Dynamic pipeline route in Go

Is it possible to create a pipeline with dynamic routes in Go? How to do it?

After watch some videos tutorial about Go, I was wondering if it was possible to create a pipeline with dynamic routes?
Inspired by Builder Design Pattern and Optional Pattern in Go, I came up with an idea to make it happen.

Imagine we are in a car factory, one car model has several types. When producing a car of the same model, there will be some of the same manufacturing steps, such as the manufacture of the chassis and body, then each type will be added a different set of features.
The pipeline that we will create has a flow diagram as shown below, after the “Body” pipeline the car can be built directly or added features according to its type.

First we will create a car structure which has several fields such as base, body, feature A, feature B, and feature C. After that, we will create several methods to fill the fields.

After that we define a variable named “Process” which is inspired by the Optional Pattern, and a struct “CarBuilder” becomes the builder for the car containing the car fields and a list of features we want to add. Then create a “nextProcess” method to move to the next pipeline and a “Build” method to return the car we built.

The last stage is defining the pipelines, we will create 5 pipelines namely “BaseBuilder”, “BodyBuilder”, “FeatureABuilder”, “FeatureBBuilder”, and “FeatureCBuilder” which serves to create the basis of the car model or add features.

To use it in main function (main.go), first we define a “waitgroup” to wait for all processes to finish, and a channel for each pipelines with big buffer to speed up :D

Also prepare a list of the types of cars we want and what features are available in those types, to facilitate the manufacture of cars on a large scale. The “NextProcess” slice contains the channels representing the features we needed to create the type.

After that, define the worker pool we want, I use 4 for each pipeline because there is no significant increase if using more than that (in my device). We can also define a car counter to determine how many cars have been made.

Define how many cars we want to make and their type

Finally, put the cars we defined above one by one into the first pipeline “BaseBuilder” and let the magic happen :D

The key is in the “Process” variable and the “nextProcess” method. In the process variable, we can input the channel of the pipeline we want to visit, while the “nextProcess” method will pass the CarBuilder pointer to the “Process” variable, which means it sends the CarBuilder pointer to the next pipeline.

Thank you!

Add a comment

Related posts:

Establishing Respect

Feels like our kids aren’t listening. Obeying. Respecting their parents. Which is natural. Yet, requires correcting. Explicit expectations. About how to act. From us Dads.

The Struggles of Coping With Depression

When you have depression, sometimes all you want to do is lie in bed and pull the covers up farther to block out the light. Do you feel this way too? If so, you may have clinical depression…

Making Sense of Prototyping for Policy

Last week I had the pleasure of spending a couple of days at the Stanford d.school thanks to the effort and hard work of Margaret Hagan, Jorge Gabriel Jiménez, and Verena Kontschieder, the host…