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
+50.5k Golang : How to get time in milliseconds?
+6.2k Golang : Gorrila mux.Vars() function example
+15.6k Golang : Capture stdout of a child process and act according to the result
+5.7k Golang : Muxing with Martini example
+3.7k JavaScript: Add marker function on Google Map
+31.2k Golang : Smarter Error Handling with strings.Contains()
+4.4k Golang : Detect words using using consecutive letters in a given string
+7.7k nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
+18.5k Golang : Reset or rewind io.Reader or io.Writer
+4.7k Golang : Configure crontab to poll every two minutes 8am to 6pm Monday to Friday
+14.8k Golang : Read integer from file into array
+17.1k Golang : How to get hour, minute, second from time?