Golang中报错"函数调用参数不足"问题解析
Golang中报错"函数调用参数不足"问题解析 大家好,我是Go语言的新手。正在尝试用Go实现批量上传到Elasticsearch的功能。我使用Go语言库 -> 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: not enough arguments in call to bulkRequest.Do
have ()
want ("golang.org/x/net/context".Context)
suresh@BLR-245:~/Desktop/tools/golang/src$
我的Go代码(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)
}
}
有人能帮我理解这个错误是什么意思以及如何解决吗? 谢谢。
更多关于Golang中报错"函数调用参数不足"问题解析的实战教程也可以访问 https://www.itying.com/category-94-b0.html
2 回复
正如错误信息所示,批量服务的Do方法需要一个context.Context参数。
Package elastic
Elastic包提供了与Elasticsearch服务器的交互接口(https://www.elastic.co/products/elasticsearch)。
你可以在这里了解context.Context的相关内容:
Package context
Context包定义了Context类型,它能够在API边界和进程之间传递截止时间、取消信号以及其他请求范围的值。
GoDoc是一个极其有用的工具,几乎包含您会遇到的所有软件包的文档。它将在您遇到棘手问题时提供帮助。
更多关于Golang中报错"函数调用参数不足"问题解析的实战系列教程也可以访问 https://www.itying.com/category-94-b0.html


