简体   繁体   中英

Getting the map value with Golang

I couldn't do it because I didn't fully understand the logic, but you can look at my code example and output below. I just want to return the country name as a variable in the output.

Json File: https://gist.githubusercontent.com/coderantidote/0894cf7c5204d4c712207ff9162d044d/raw/ab9ec19dcfecd93addb4b1961a2506b34164c090/tld

 package main import ( "fmt" "strings" tld "github.com/jpillora/go-tld" gojsonq "github.com/thedevsaddam/gojsonq/v2" ) func main() { urls:= []string{ "http://google.com", "https://eduuk.co.uk/", "https://www.medi-cal.ca.gov/", } for _, url:= range urls { u, _:= tld.Parse(url) jq:= gojsonq.New().File("./tld.json").From("tlds") tldSlice:= strings.Split(u.TLD, ".") if len(tldSlice) == 2 { jq.Where("fields.tld", "=", tldSlice[1]).Select("fields.country") } else { jq.Where("fields.tld", "strictContains", tldSlice[0]).Select("fields.country") } fmt.Printf("%s\n", u.TLD) fmt.Printf("%v\n", jq.Get()) } }

Output:

com
[map[country:Commercial organizations]]
co.uk
[map[country:United Kingdom]]
gov
[map[country:US government]]

Is this what you are looking for?

fmt.Printf("%s\n", u.TLD)
fmt.Printf("%v\n", jq.First().(map[string]interface{})["country"])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM