英文:
Golang error "not enough arguments in call"
问题
我是你的中文翻译助手,以下是你要翻译的内容:
我对golang还不熟悉。尝试使用golang实现向Elasticsearch进行批量上传。我正在使用golang库-> https://github.com/olivere/elastic 与Elasticsearch进行通信。
此外,我尝试了一段示例代码,但是出现了以下错误...
suresh@BLR-245:~/Desktop/tools/golang/src$ go install github.com/crazyheart/elastic-bulk-upload
github.com/crazyheart/elastic-bulk-upload
github.com/crazyheart/elastic-bulk-upload/main.go:29: 在bulkRequest.Do的调用中参数不足
have ()
want ("golang.org/x/net/context".Context)
suresh@BLR-245:~/Desktop/tools/golang/src$
我的Golang代码(main.go)
package main
import (
"fmt"
"gopkg.in/olivere/elastic.v5"
"strconv"
)
type Tweet struct {
User string json:"user"
Message string json:"message"
}
func main() {
client, err := elastic.NewClient()
if err != nil {
fmt.Println("%v", err)
}
n := 0
for i := 0; i < 1000; i++ {
bulkRequest := client.Bulk()
for j := 0; j < 10000; j++ {
n++
tweet := Tweet{User: "olivere", Message: "Package strconv implements conversions to and from string representations of basic data types. " + strconv.Itoa(n)}
req := elastic.NewBulkIndexRequest().Index("twitter").Type("tweet").Id(strconv.Itoa(n)).Doc(tweet)
bulkRequest = bulkRequest.Add(req)
}
bulkResponse, err := bulkRequest.Do()
if err != nil {
fmt.Println(err)
}
if bulkResponse != nil {
}
fmt.Println(i)
}
}
有人能帮我理解这个错误的含义以及如何解决吗?
英文:
I'm new to golang. Trying to implement a bulk upload to Elasticsearch by golang. I'm using golang library -> https://github.com/olivere/elastic for communication with Elasticsearch.
Also, a piece of sample code which I'm trying but getting following error...
suresh@BLR-245:~/Desktop/tools/golang/src$ go install github.com/crazyheart/elastic-bulk-upload
# github.com/crazyheart/elastic-bulk-upload
github.com/crazyheart/elastic-bulk-upload/main.go:29: not enough arguments in call to bulkRequest.Do
have ()
want ("golang.org/x/net/context".Context)
suresh@BLR-245:~/Desktop/tools/golang/src$
My Golang Code(main.go)
package main
import (
"fmt"
"gopkg.in/olivere/elastic.v5"
"strconv"
)
type Tweet struct {
User string `json:"user"`
Message string `json:"message"`
}
func main() {
client, err := elastic.NewClient()
if err != nil {
fmt.Println("%v", err)
}
n := 0
for i := 0; i < 1000; i++ {
bulkRequest := client.Bulk()
for j := 0; j < 10000; j++ {
n++
tweet := Tweet{User: "olivere", Message: "Package strconv implements conversions to and from string representations of basic data types. " + strconv.Itoa(n)}
req := elastic.NewBulkIndexRequest().Index("twitter").Type("tweet").Id(strconv.Itoa(n)).Doc(tweet)
bulkRequest = bulkRequest.Add(req)
}
bulkResponse, err := bulkRequest.Do()
if err != nil {
fmt.Println(err)
}
if bulkResponse != nil {
}
fmt.Println(i)
}
}
Anybody, help me to understand what that error means & how to solve those?
答案1
得分: 4
你需要将上下文传递给bulkRequest.Do()方法。
从olivere/elastic的Github页面(简化版):
ctx := context.Background()
bulkRequest.Do(ctx)
英文:
You need to pass a context to bulkRequest.Do().
From the olivere/elastic Github page (abbreviated);
ctx := context.Background()
bulkRequest.Do(ctx)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论