Build Job Survey CLI Tool with Go Using Strategy Pattern by Dilara Goram | October, 2022

A portal to decide your next job

When I was in high school, our mentor teacher asked us to take part in a survey to decide which department would be the best fit for us based on our interests and our grades. Don’t worry! I will not discuss the absurdity of that kind of approach.

However, I need to say that your personality, abilities and mindset can evolve and change over time. This development depends on your readiness for development. do not forget! As said by csdweek in Mindset: A failure doesn’t define you. It’s just a problem you need to face, deal with and learn.

Therefore, this type of survey cannot make a final decision about you, especially if it depends on your grades.

Having dismissed that kind of flawed survey logic, it is strange to say that this article is about the Survey CLI tool.

Working demo: https://asciinema.org/a/531177Source Code: https://github.com/dilaragorum/job-surveyCobra: Help us to develop a very basic CLI tool.Survey: Help us to ask questions and get answers from users.

The first question in the survey would be, “Which job do you want to do?” At present, there are three job options (Astronaut, Botanist and Software Developer).

You will face various questions regarding the job you want depending on your preference. For example, if you choose Astronaut, you’ll see these questions below.

What is the biggest challenge of this project? what do you think

There are different jobs, different questions and evaluation techniques. So we should be able to assist in adding new job options, new questions, etc.

If we can understand the problem clearly, how can we solve this problem efficiently? here you go…

Meet our friend Strategy Pattern.

When this pattern is implemented in our project, all jobs have their own strategy methods, and our program will decide which strategy works to your liking.

Figure 2: Applied Strategy Pattern

job checker interface

type JobChecker interface {
Check(answersBytes []byte) (CheckResult, error)
GetQuestions() []*survey.Question
}

We implement this interface with all job structures, as shown below.

  1. astronaut.go

2. botanist.go

3. software_developer.go

we used map To store all the strategies in one place. Based on your choice, we are studying this map and finding suitable strategy.

We used the map key as the task name and the value as the task structure, as shown below. In the initialization step, we used go init way.

Our program used this map to find the right job structure and implement this structure CheckAnd GetQuestionsmethods and return the result to the user.

questions := strategy.Job.GetQuestions(job)// ..message, err := strategy.Job.Check(desired.Job, answersBytes)
if err != nil {
return err
}

As such, our program has no idea about our concrete strategies in our job structures.

Therefore, by providing loose coupling, our codebase remains easy to maintain and expandable.

Leave a Comment