有一个新手问我如何在记录列表(数据结构)中搜索特定的项目,下面是一个简单的例子,说明如何使用Golang搜索或迭代记录列表。
这个简单的程序所做的是演示如何在一个记录列表中进行迭代,并寻找一个符合搜索键的特定名称。一旦找到该记录,就返回该记录进行进一步处理。
在这里,你可以
package main
import (
"fmt"
"strings"
)
type citySize struct {
Name string
Population float64
}
// cities with the largest population in the world
var citiesList = []citySize{
{"Tokyo", 38001000},
{"Delhi", 25703168},
{"Shanghai", 23740778},
{"Sao Paulo", 21066245},
}
func findRecordsByCityName(name string) bool {
for _, v := range citiesList {
// convert to lower case for exact matching
if strings.ToLower(v.Name) == strings.ToLower(name) {
return true
}
}
return false
}
func returnRecordsByCityName(name string) citySize {
for _, v := range citiesList {
// convert to lower case for exact matching
if strings.ToLower(v.Name) == strings.ToLower(name) {
return v
}
}
return citySize{"", 0}
}
func main() {
if !findRecordsByCityName("New York") {
fmt.Println("New York is not in the list!")
}
// Check if Tokyo is in the list
cityData := returnRecordsByCityName("Tokyo")
if cityData.Name != "" {
fmt.Println(cityData)
}
// Check if Teluk Intan is in the list
cityData = returnRecordsByCityName("Teluk Intan")
if cityData.Name != "" {
fmt.Println(cityData)
} else {
fmt.Println("Teluk Intan is not in the list")
}
}
输出
New York is not in the list!
{Tokyo 3.8001e+07}
Teluk Intan is not in the list
编码愉快!
参考资料