I saved this as a snippet for vscode to get up and running quickly with something better than the defaults for handling
func main isolation. I’ve been working on modifying this a bit as I don’t really like using args, but am trying not to overcomplicate things as a new gopher.
I tend to like better flag parsing than using args, but it’s still a better pattern to get functions isolated from main to easily test.
The gist that I’ve taken from this and discussions in the community is ensure that
main is where program termination is dedicated instead of handling this in your functions. This isolation of logic from main ensures you can more easily setup your tests as well, since
func main() isn’t testable.
Puzzles - FizzBuzz
I honestly had never done any algorithm or interview puzzles beyond sql-server, so I was really happy to knock this out relatively easily. At least I pass the basic Joel test 😁
At this stage, I’m using zerolog as I found it very easy to get moving with structured logs.
The output of this demo looks pretty good!
Here’s a functional demo that can be used to bootstrap a new project with this.
Algorithms & Challenges
I’ll eventually put these in a repo most likely, and have test cases to make runnable, but for now, just snippets of solutions to reference.
Alex works at a clothing store. There is a large pile of socks that must be paired by color for sale. Given an array of integers representing the color of each sock, determine how many pairs of socks with matching colors there are. Return the total number of matching pairs of socks that Alex can sell.
Given an array of integers, sort the array in ascending order using the Bubble Sort algorithm above. Once sorted, print the following three lines for example: Array is sorted in 3 swaps. First Element: 1 Last Element: 6