Golang : Extract or copy items from map based on value
Problem:
You want to copy map or make a new map with subset or extract items from another map based on certain value. For example, only copy values higher than 200.00 or with string values that match a slice's elements. Kinda like SQL select statement based on certain criteria.
Solution:
The code example below should be self explanatory.
package main
import "fmt"
func main() {
stockPrices := map[string]float32{
"IBM": 85.23,
"HPQ": 399.00,
"AAPL": 712.00,
"MSFT": 123.00,
}
fmt.Println("---------------------------------------------------------------------------")
fmt.Println("Original map content")
fmt.Println("---------------------------------------------------------------------------")
for k, v := range stockPrices {
fmt.Printf("key : %v, value : %v \n", k, v)
}
fmt.Println("---------------------------------------------------------------------------")
fmt.Println("New map subset with prices higher than 200")
fmt.Println("---------------------------------------------------------------------------")
higherStockPrices := map[string]float32{}
// copy map value with prices higher than 200
// to new map
for name, price := range stockPrices {
if price > 200.00 {
fmt.Println(name, price)
higherStockPrices[name] = price
}
}
//fmt.Println(higherStockPrices)
for higherName, higherPrice := range higherStockPrices {
fmt.Printf("key : %v , value : %v \n", higherName, higherPrice)
}
fmt.Println("---------------------------------------------------------------------------")
fmt.Println("New map subset with stock names that are found in slice")
fmt.Println("---------------------------------------------------------------------------")
stockNames := []string{"HPQ", "MSFT"}
for name, price := range stockPrices {
for _, sName := range stockNames {
if name == sName {
fmt.Println(name, price)
}
}
}
}
Output :
-------------------------------------------------------------------------------
Original map content
-------------------------------------------------------------------------------
key : IBM, value : 85.23
key : HPQ, value : 399
key : AAPL, value : 712
key : MSFT, value : 123
-------------------------------------------------------------------------------
New map subset with prices higher than 200
-------------------------------------------------------------------------------
HPQ 399
AAPL 712
key : HPQ , value : 399
key : AAPL , value : 712
-------------------------------------------------------------------------------
New map subset with stock names that are found in slice
-------------------------------------------------------------------------------
HPQ 399
MSFT 123
References:
See also : Golang : How to delete element(data) from map ?
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
+20.5k Golang : Convert date string to variants of time.Time type examples
+6.2k PHP : Proper way to get UTF-8 character or string length
+5.3k How to check with curl if my website or the asset is gzipped ?
+9.9k Golang : How to tokenize source code with text/scanner package?
+11.7k Golang : Determine if time variables have same calendar day
+9.3k Golang : Convert(cast) string to int64
+13.1k Golang : Verify token from Google Authenticator App
+7.3k Android Studio : AlertDialog to get user attention example
+16.3k Golang : Delete files by extension
+20.2k Golang : Pipe output from one os.Exec(shell command) to another command
+22.9k Golang : Randomly pick an item from a slice/array example
+8.1k Golang: Prevent over writing file with md5 hash