在下面的代碼中,我想放一個變量id,就像一些格式說明符。如何使用 Golang 對以下彈性搜索查詢執(zhí)行此操作?%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可以工作,但也容易出錯。我會創(chuàng)建適當的結構,然后用它來做: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 關注
- 76 瀏覽
添加回答
舉報
0/150
提交
取消