Golang中如何在多层map的[]string里搜索特定值
Golang中如何在多层map的[]string里搜索特定值
如何从 map[string]map[string][]string 中搜索值
我尝试了 C++ 的方法但没有成功
func FindMapByKey(m map[string][]string, value string) bool {
for _, val := range m {
for _, n := range val {
if strings.EqualFold(value, n) {
return true
}
}
}
return false
}
我尝试通过提供内部键来搜索值。也尝试使用三重循环,这可能更有意义,但不知道如何使用该函数。三重循环代码的使用
func FindMapByKey(m map[string]map[string][]string, value string) bool {
for _, val := range m {
for _, innerVal := range val {
for _, n := range innerVal {
if strings.EqualFold(value, n) {
return true
}
}
}
}
return false
}
v := FindMapByKey(list.PaymentCmd["PaymentMethod"]["Online"], "Paypal") 但我遇到了错误
更多关于Golang中如何在多层map的[]string里搜索特定值的实战教程也可以访问 https://www.itying.com/category-94-b0.html
是的
如果我理解正确的话,你只需要使用我的 Contains 函数和你的数据:
Contains(mp[book][payment], provider)
你是这个意思吗?
Alex_Go:
但是我遇到了错误
具体是什么错误?
我在 Go Playground 上尝试了你的第一个示例,看起来可以正常运行。Go Playground - Go 编程语言
第二个示例似乎也能正常工作:Go Playground - Go 编程语言
已解决
func contains(mp map[string]map[string][]string, val string, outterKey string, innerKey string) bool {
if outter, found := mp[outterKey]; found {
if inner, foundTwo := outter[innerKey]; foundTwo {
for _, n := range inner {
if strings.EqualFold(val, n) {
return true
}
}
}
}
return false
}
Alex_Go:
问题在于如何在代码中声明一个变量,使其能够使用具有内外层映射特定参数的代码
抱歉,我完全没理解你在这里想表达什么……
但从你下面展示的代码来看,你似乎不是想检查给定的字符串是否存在于嵌套映射中,而是想知道它是否存在于切片中。
func Contains(haystack []string, needle string) bool {
for _, v := range haystack {
if v == needle {
return true
}
}
return false
}
以下是对这个问题的更好解释。
我的任务是:如果用户选择了产品,例如Book1,并选择了支付方式(在线支付),同时选择PayPal作为支付提供商。现在我需要检查用户选择的产品是否支持该支付方式(Book1),其他产品也同理。每个产品都有自己独立的支付方式。
由于我有用户提供的外部键(Book1)和内部键(在线支付)参数,我需要检查提供的支付方式(PayPal)是否存在于该产品的映射中。
所以我的问题是如何为条件if语句声明变量,并提供从用户那里接收的参数。更好的做法是创建一个接受额外参数的函数,还是可以像普通映射那样通过mp["Book1"]提供参数?
func main() {
fmt.Println("hello world")
}
这是我的错误。在声明 v := FindMapByKey(list.PaymentCmd["PaymentMethod"]["Online"], "Paypal") 时出了问题。我尝试声明 v := FindMapByKey(list.PaymentCmd, "Paypal") 后就能正常工作了。但现在又出现了另一个问题:这个搜索会遍历映射中所有相同的值。
mp := map[string]map[string][]string{
"Book1": {
"Online": {
"paypal", "YandexMoney",
},
"CreditCard": {
"Visa", "MasterCard",
},
},
"Book2": {
"Online": {
"paypal",
},
"CreditCard": {
"Visa",
},
},
}
问题在于如何在使用代码时声明一个变量,指定内部和外部映射的参数
v := FindMapByKey(list.PaymentCmd["Book1"]["Online"], "Paypal") 会触发错误
不能在 FindMapByKey 参数中使用 mp[“Book1”][“Online”](类型 []string)作为类型 map[string]map[string][]string
在Golang中搜索多层嵌套的map结构需要正确遍历每一层。你的三重循环方法基本正确,但调用方式有问题。以下是完整的解决方案:
package main
import (
"fmt"
"strings"
)
// 搜索三层嵌套map中的特定值
func FindInNestedMap(m map[string]map[string][]string, searchValue string) bool {
for _, outerMap := range m {
for _, innerSlice := range outerMap {
for _, value := range innerSlice {
if strings.EqualFold(value, searchValue) {
return true
}
}
}
}
return false
}
// 更灵活的版本,可以返回找到的完整路径
func FindInNestedMapWithPath(m map[string]map[string][]string, searchValue string) (bool, []string) {
for outerKey, outerMap := range m {
for innerKey, innerSlice := range outerMap {
for _, value := range innerSlice {
if strings.EqualFold(value, searchValue) {
return true, []string{outerKey, innerKey, value}
}
}
}
}
return false, nil
}
func main() {
// 示例数据结构
data := map[string]map[string][]string{
"PaymentMethod": {
"Online": {"Paypal", "Stripe", "CreditCard"},
"Offline": {"Cash", "Check", "BankTransfer"},
},
"ShippingMethod": {
"Express": {"FedEx", "UPS", "DHL"},
"Standard": {"USPS", "LocalPost"},
},
}
// 使用三重循环搜索
found := FindInNestedMap(data, "Paypal")
fmt.Printf("找到Paypal: %t\n", found) // 输出: 找到Paypal: true
found = FindInNestedMap(data, "Bitcoin")
fmt.Printf("找到Bitcoin: %t\n", found) // 输出: 找到Bitcoin: false
// 使用带路径的搜索
found, path := FindInNestedMapWithPath(data, "Paypal")
if found {
fmt.Printf("找到值,路径: %v\n", path) // 输出: 找到值,路径: [PaymentMethod Online Paypal]
}
// 直接在代码中遍历的示例
searchValue := "DHL"
for outerKey, outerMap := range data {
for innerKey, innerSlice := range outerMap {
for _, value := range innerSlice {
if strings.EqualFold(value, searchValue) {
fmt.Printf("在 %s -> %s 中找到 %s\n", outerKey, innerKey, value)
}
}
}
}
}
你的调用方式错误在于试图直接访问特定路径而不是遍历整个结构。正确的调用应该是:
data := map[string]map[string][]string{
"PaymentCmd": {
"PaymentMethod": {"Online", "Offline"},
},
}
// 正确调用 - 传入整个map进行搜索
found := FindInNestedMap(data, "Paypal")
或者如果你知道确切路径,可以直接访问:
// 如果你确定路径存在,可以直接访问
if paymentMethods, exists := data["PaymentCmd"]; exists {
if onlineMethods, exists := paymentMethods["PaymentMethod"]; exists {
for _, method := range onlineMethods {
if strings.EqualFold(method, "Paypal") {
fmt.Println("找到Paypal")
}
}
}
}
三重循环的方法适用于不确定值在哪个嵌套层级的情况,而直接路径访问适用于已知确切结构的情况。

