Golang : How to filter a map's elements for faster lookup



Tags : golang filter-element map lookup

A short tutorial on how to filter map elements. Map is a hash table equivalent in Golang and very useful for doing fast lookup. However, there are times we need to "scale" down the number of elements in a map for a "faster" lookup(smaller set to search). One way to do this is to filter out the elements that we need or don't need.

Below is an example code on how to filter elements out from a map.

Here you go!


 package main

 import (
 "fmt"
 "strings"
 )

 func main() {

 hostings := map[int]string{0: "digitalocean.com",
 1: "linode.com",
 2: "azure.com",
 3: "aws.amazon.com",
 4: "heroku.com"}

 fmt.Println("Before filtering :")
 fmt.Println("-------------------------")
 for number, company := range hostings {
 fmt.Println("Number : ", number, "Company : ", company)
 }

 fmt.Println("\n\nFilter by index number. Need to know index number in advance :")
 fmt.Println("-------------------------")
 companyName, ok := hostings[3] // select index number 3

 if ok {
 fmt.Println("Hosting company name : ", companyName)
 } else {
 fmt.Println(companyName + " not found")
 }

 // filter aws.amazon.com from the hostings map

 fmt.Println("\n\nFilter map elements by name :")
 fmt.Println("-------------------------")
 for number, company := range hostings {
 if strings.EqualFold(strings.ToLower(company), "aws.amazon.com") {
 fmt.Printf("[%d] : %s\n", number, company)
 }
 }

 // filter company names that contain the character "u" from the hostings map
 fmt.Println("\n\nFilter map elements by a single character :")
 fmt.Println("-------------------------")
 for number, company := range hostings {
 if strings.Contains(strings.ToLower(company), "u") {
 fmt.Printf("[%d] : %s\n", number, company)
 }
 }

 }

Sample output:

Before filtering :

-------------------------

Number : 3 Company : aws.amazon.com

Number : 4 Company : heroku.com

Number : 0 Company : digitalocean.com

Number : 1 Company : linode.com

Number : 2 Company : azure.com

Filter by index number. Need to know index number in advance :

-------------------------

Hosting company name : aws.amazon.com

Filter map elements by name :

-------------------------

3 : aws.amazon.com

Filter map elements by a single character :

-------------------------

4 : heroku.com

2 : azure.com

References:

https://www.socketloop.com/tutorials/golang-of-hash-table-and-hash-map

https://www.socketloop.com/tutorials/golang-check-if-element-exist-in-map

https://www.socketloop.com/tutorials/golang-how-to-check-if-a-string-contains-another-sub-string

https://www.socketloop.com/tutorials/golang-remove-characters-from-string-example



Tags : golang filter-element map lookup

By Adam Ng

IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.


Advertisement