在下面的代码中,我想放一个变量id,就像一些格式说明符。如何使用 Golang 对以下弹性搜索查询执行此操作?%dstr := `{ "query": { "match": { "id": 123 } } }` s := []byte(str)url := "http://localhost:9200/student/_delete_by_query"_, err = helper.GoDelete(url, s)if err != nil { return err}return nil
2 回答

慕娘9325324
TA贡献1783条经验 获得超4个赞
使用可能是最简单的方法,而不是最快的方法。但最简单。fmt.Sprintf
d := 123 id := fmt.Sprintf(`{"query": {"match": {"id": %d}}}`, d)

慕斯王
TA贡献1864条经验 获得超2个赞
fmt.Sprintf可以工作,但也容易出错。我会创建适当的结构,然后用它来做:json.Marshal
type (
Match struct {
ID int `json:"id"`
}
Query struct {
Match Match `json:"match"`
}
MyStruct struct {
Query Query `json:"query"`
}
)
func main() {
s := MyStruct{
Query: Query{
Match: Match{
ID: 123,
},
},
}
bytes, err := json.Marshal(s)
}
- 2 回答
- 0 关注
- 62 浏览
添加回答
举报
0/150
提交
取消