Order map in Go

As you may know, it’s not possible to order a map in Go (just like in any language where maps are implemented as hash maps). However, we can achieve the same result by working with the map’s keys.

The approach is to extract the keys into a slice, sort them, and then retrieve the corresponding values from the map using the sorted keys.

Let’s take a common example: given a list of strings, return the list of strings sorted in decreasing order of their frequency.

func orderByFrequency(slice []string) []string {
	occurrenceMap := make(map[string]int, len(slice))

	for _, elem := range slice {
		occurrenceMap[elem]++
	}
	keys := make([]string, 0, len(occurrenceMap))

	for key := range occurrenceMap {
		keys = append(keys, key)
	}
	sort.SliceStable(keys, func(i, j int) bool {
	     return occurrenceMap[keys[i]] > occurrenceMap[keys[j]] 
	})

	return keys
}

In this approach, we first create a map to count the occurrences, then create a slice of the keys and sort them.

But what happens if we want to add another sorting criterion? For example, if two strings have the same occurrence count, we want to sort them in decreasing order by their string value.

In this case, we modify the sorting function as follows:

sort.SliceStable(keys, func(i, j int) bool {
		if occurrenceMap[keys[i]] > occurrenceMap[keys[j]] {
			return true
		}
		if occurrenceMap[keys[i]] == occurrenceMap[keys[j]] && keys[i] > keys[j] {
			return true
		}
		return false
	})

Try to create an exercise yourself.

Cogentcore

On July 24th, the first release of Cogent (https://www.cogentcore.org/), a new Go GUI cross-platform tool (Windows, OSX, Linux, Wasm, iOS, Android), was launched. This tool uses only Go language to build interfaces, with no templates or other files (like YAML, JSX), and the styling is done directly in the code. It follows the Material style.

What do I like about this project?

  • Clear goal
  • How to achieve the goal
  • The styling

What don’t I like?

  • I think that the best solution for GUI tools is a combination of declarative and imperative languages, like Qt (Qml + C++ or JavaScript), SwiftUI + Swift, and Slint, which also has an approach like this. Using this tool feels like going back to the time of Qt Widget.
  • For mobile, it uses Material Design, but I think that for mobile, it is essential to respect the native user interface.

For a first release, it looks good. I will keep it under observation and recommend you do the same.

Exploring Fyne: A Modern Cross-Platform Tool for Golang Developers

I don’t know if you’ve ever heard about Fyne (https://fyne.io/), the Golang cross-platform tool. I spent some hours trying it out. It runs on Windows, Linux, Darwin, Android, and iOS. The comparison with other cross-platform tools is inevitable, but it can also be unfair considering the age of the tool. With it, we can build very good desktop applications; more or less, we have all the fundamental GUI objects for a desktop application, and if you don’t need a Hollywood-style graphic, it’s good.

About the mobile situation, it’s a bit different. Mobile applications (apart from games) should conform to native guidelines, which is a bit hard with Fyne since it follows Material Design. What I’m saying is not a dismissal, but my advice is to use it if you need basic (or industrial) applications, where a nice user interface is less important than usual.

What’s my suggestion about it? If you need to build a desktop application (or mobile without a native look and feel), you already know Go, and you don’t want to spend time on other languages, then go for it. If you don’t like something, you can always contribute to the project—actions speak louder than words.

Note: English is not my native language, so I apologize for any errors. I use AI solely to generate the banner of the post; the content is human-generated.