Golang错误 “not enough arguments in call” 的中文翻译是 “调用中参数不足”。

huangapple go评论105阅读模式
英文:

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 (&quot;golang.org/x/net/context&quot;.Context)
suresh@BLR-245:~/Desktop/tools/golang/src$ 

My Golang Code(main.go)

package main

import (
	&quot;fmt&quot;
	&quot;gopkg.in/olivere/elastic.v5&quot;
	&quot;strconv&quot;
)

type Tweet struct {
	User    string `json:&quot;user&quot;`
	Message string `json:&quot;message&quot;`
}

func main() {
	client, err := elastic.NewClient()
	if err != nil {
		fmt.Println(&quot;%v&quot;, err)
	}

	n := 0
	for i := 0; i &lt; 1000; i++ {
		bulkRequest := client.Bulk()
		for j := 0; j &lt; 10000; j++ {
			n++
			tweet := Tweet{User: &quot;olivere&quot;, Message: &quot;Package strconv implements conversions to and from string representations of basic data types. &quot; + strconv.Itoa(n)}
			req := elastic.NewBulkIndexRequest().Index(&quot;twitter&quot;).Type(&quot;tweet&quot;).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)

huangapple
  • 本文由 发表于 2017年3月13日 15:30:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/42758392.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定