Golang : How to filter a map's elements for faster 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
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
Tutorials
+10.3k RPM : error: db3 error(-30974) from dbenv->failchk: DB_RUNRECOVERY: Fatal error, run database recovery
+10.8k Golang : Create Temporary File
+14.2k Golang : Parsing or breaking down URL
+6.5k Golang : Derive cryptographic key from passwords with Argon2
+10.1k Golang : Wait and sync.WaitGroup example
+22.7k Golang : Calculate time different
+13.4k Golang : Query string with space symbol %20 in between
+12.2k Golang : Extract part of string with regular expression
+29.1k Golang : How to create new XML file ?
+6.9k Golang : Squaring elements in array