1 回答
TA贡献1839条经验 获得超15个赞
如果您需要打印密钥,也许您可以使用map. 你可以打印键和map值。
package main
import "fmt"
func main() {
var name string
value1 := map[string]string{"name": "samename", "email": "email one", "role": "job role one"}
value2 := map[string]string{"name": "samename", "email": "email two", "role": "job role two"}
values := []map[string]string{value1, value2}
var found bool
for {
fmt.Printf("Enter your name: ")
fmt.Scanln(&name)
fmt.Println()
for _, value := range values {
if value["name"] == name {
found = true
for k, v := range value {
fmt.Printf("Here is the %s: %s\n", k, v)
}
fmt.Println()
}
}
if found {
break
} else {
fmt.Println("Name is not found. Try again!")
}
}
}
- 1 回答
- 0 关注
- 71 浏览
添加回答
举报