Golang: Three common programming Problems

Saurabh Nayar
Higher-Order Functions
4 min readJun 27, 2020

--

And three not very very obvious Golang solutions

Every language is unique. Solutions for these common programming challenges were very different in Java — my previous favorite programming language. And if I dare to say so, the solutions to these problems were more intuitive in Java.

Golang has unique ways to solve these problems. The solutions I listed below were not very intuitive for me initially — but have become second nature now. I am not sure whether they are “idiomatic GO” — frankly, I don’t know what idiomatic GO is.

And there may be better, different ways to solve these problems — I would love to hear your ideas.

Golang: Tips & Tricks

So, let’s dive right into the programming challenges.

Problem #1: I need to maintain a “Set” of items. But oh no, Golang doesn’t have a “Set” data structure.

One of the Solutions: Golang doesn’t have “Sets” but it has “Maps”. The Key-set of a map is a unique set of items.

You can do something like below (https://play.golang.com/p/tayo3H5mi56):

package mainimport "fmt"type Set struct {
m map[string]bool
}
func NewSet() Set {
m := make(map[string]bool)
return Set{m: m}
}

--

--