【发布时间】:2018-06-15 21:29:04
【问题描述】:
我正在为搜索功能编写服务。当我在正文中传递值以获取特定记录时,我只能根据 PHONE 的结构值获取它。
我对 golang 很陌生。
我需要使用结构中的所有字段来搜索 ex.phone 或患者结构的名字或姓氏的值
我的结构如下
type PatientEntity struct {
ID int64
FirstName string
LastName string
Phone string
}
我的代码是
func SearchPatientsHandler(res http.ResponseWriter, req *http.Request) {
patient := &PatientEntity{}
if err := json.NewDecoder(req.Body).Decode(patient); err != nil {
panic(err)
}
patients := []*PatientEntity{}
q := datastore.NewQuery(KINDPATIENT).Filter("FirstName =",patient.FirstName)
keys, err := q.GetAll(ctx, &patients)
if err != nil {
// Handle error
return
}
json.NewEncoder(res).Encode(patients)
}
我需要搜索结构的所有值。我该如何解决。
【问题讨论】:
-
这篇文章包含了完成这个任务所需的一切,并附有示例代码:Index Selection and Advanced Search
-
请在
NewQuery之前显示patient的值,在GetAll之后显示patients,KINDPATIENT的值。您可能还想查询数据存储以确保您的数据符合预期。 -
我想使用 PHONE 或 FIRSTNAME 或 LASTNAME 过滤值。是否可以使用 FILTER 使用 DATASTORE。
标签: google-app-engine go datastore